Saturday, April 20, 2013

Closure wrapper to avoid global variables in javascript

The easiest way is to wrap your code in a closure and manually expose only those variables you need globally to the global scope:

(function() {
  // Your code here

  // Expose to global
  window['varName'] = varName;
})();

No comments:

Post a Comment