namespaces - Namespacing Variables - javascript -


i see lot of namespacing examples functions but, o.k. declare variables (global program) in way?

var mynamespace = {}; mynamespace.var1 = 5;

or should variables placed in functions within namespace?

you should avoid global variables... use sort of module pattern instead, e.g.

(function () {    "use strict";    var myvar = 'blob'; }()); 

see http://yuiblog.com/blog/2007/06/12/module-pattern/

edit:

more clarification:

var ns1 = ns1 || {}; ns1.mymodule = function () {         "use strict";         var myvar = 'blob';         return  {             mypublicmethod: function () {                 return myvar;             }         };   }(); 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -