Friday, April 26, 2013

JavaScript best practices

I am trying not to post trivial information, but I have some notes on JavaScript best practices and it seems reasonable to put this in my blog. This is far from complete, but contains some important points.

JavaScript should be contained in .js files and not be in markup files or be generated by code. The allows the JavaScript to be cached. It also makes it easier to understand since the JavaScript is all in a known location and not spread around in the code with server code potentially writing client code.

JavaScript should be contained in modules (look up the Module pattern in JavaScript). This helps group together functionality and also serves to protect the global namespace from pollution. Modules, because they are insulated from the global namespace, minify well.

Events should be handled using jQuery's "on" function. This is considered a jQuery best practice.

No comments:

Post a Comment