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
Post a Comment