(New page: This page lists some interesting coding patterns that we may or may not use in our code. == Self Executing Functions == This is already a classic. Using a self calling function declarati...) |
|||
| Line 45: | Line 45: | ||
... | ... | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</pre> | </pre> | ||
Latest revision as of 15:44, 13 September 2010
This page lists some interesting coding patterns that we may or may not use in our code.
Self Executing Functions
This is already a classic. Using a self calling function declaration to isolate its contents from the current scope, avoiding polluting it with variables.
(function()
{
...
})();
Objects with Private Stuff
var myObj = function()
{
// Private variables.
var privateVar = 10;
var privateFunction = function()
{
...
};
// Public stuff.
return {
publicMethod : function()
{
// May access privateVar or privateFunction.
...
}
};
}();
For Loops and Length
The "length" property should not be checked on each "for" cycle. The following construction should be used instead:
for ( var i = 0, len = list.length ; i < len ; i++ }
{
...
}