Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Best Practices [closed]

People also ask

Are closures good practice?

The short answer is that, used judiciously, closures are valuable, and often the best way to perform a task.

Are JavaScript closures important?

Closures are important because they control what is and isn't in scope in a particular function, along with which variables are shared between sibling functions in the same containing scope.

What are the disadvantages of closures in JavaScript?

There are two main disadvantages of overusing closures: The variables declared inside a closure are not garbage collected. Too many closures can slow down your application. This is actually caused by duplication of code in the memory.


Seconding Javascript: The Good Parts and Resig's book Secrets of the Javascript Ninja.

Here are some tips for Javascript:

  • Don't pollute the global namespace (put all functions into objects/closures)
    • Take a look at YUI, it's a huge codebase with only 2 global objects: YAHOO and YAHOO_config
  • Use the Module pattern for singletons (http://yuiblog.com/blog/2007/06/12/module-pattern/)
  • Make your JS as reusable as possible (jQuery plugins, YUI modules, basic JS objects.) Don't write tons of global functions.
  • Don't forget to var your variables
  • Use JSlint : http://www.jslint.com/
  • If you need to save state, it's probably best to use objects instead of the DOM.

I disagree to the "use a framework" statement to some degree. Too many people use frameworks blindly and have little or no understanding of what's going on behind the curtains.


I liked JavaScript:The Good Parts by Douglas Crockford although it's focused entirely on the language and ignores the DOM altogether.


If you don't feel like reading you can watch this video: JavaScript the good parts by Doug Crockford.