Javascript Best Practices from http://www.slideshare.net/cheilmann/javascript-best-practices-1041724
无意之中看到这篇文章,想到自己平时写JS时都不注意这些细节,把其中一些有用的记下
Avoid globals,Global variables are a terribly bad idea.You run the danger of your code being overwritten by any other javascript added to the page after yours. the workaround is to use closures and the module pattern.
Everything is global and can be accessed
Problem:access is not contained,anything in the page can overwrite what you do.
Object Literal: Everything is contained but can be accessed via the object name.
Problem:Repetition of module name leads to huge code and is annoying.
Anonymous Module Nothing is global.
Problem:No access from the outside at all (callbacks,event handlers)
Module Pattern: You need to specify what is global and what isn't-switching syntax in between.
Problem:Repetition of module name,different syntax for inner functions.
Revealing Module Pattern:Keep consistent syntax and mix and match what to make global.
Shortcut notatitions keep your code snappy and easier to read once you got used to it.
is the same as
is the same as
is the same as
is the same as
By putting these into a configuration object and making this one public we make maintenance very easy adn allow for customization.
This means that every time the loop runs,JavaScript needs to read the length of the array.You can avoid that by storing the length value in a different variable:
An even shorter way of achieving this is to create a second variable in the preloop condition