Thursday 4 February 2010

Javascript Classes

The problem with having a lot of javascript functions on your website is that you can begin to forget what each function refers to, and which function names you have already used previously.

For example, if I was creating some javascript for my website's main navigation bar, I might have a function which opens/closes a sub navigation list called 'toggleSubNav'. If the website has a product categories section, I might also need a different javascript function, and there is a chance I could also call this 'toggleSubNav'.

Using javascript classes allows you to better organise your functions. So if the basic outline for my main navigation function was:

function toggleSubNav()
{
}

This could be changed to:

var mainNavigation =
{
toggleSubNav: function ()
{
}
}


Then I would use mainNavigation.toggleSubNav when referring to this function in my HTML or another javascript function.

No comments:

Post a Comment