I see a lot of namespacing examples with functions but, is it o.k. to declare variables (global to my program) in this way?
var mynamespace = {}; mynamespace.var1 = 5;
or should all variables be placed in functions within the namespace?
I see a lot of namespacing examples with functions but, is it o.k. to declare variables (global to my program) in this way?
var mynamespace = {}; mynamespace.var1 = 5;
or should all variables be placed in functions within the namespace?
You should avoid global variables... Use some 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;
}
};
}();