I like the idea of using Singleton mentioned here .html:
var Namespace = {
Util: {
util_method1: function() {…},
util_method2: function() {…}
},
Ajax: {
ajax_method: function() {…}
},
some_method: function() {…}
};
Let's say I have to add some methods and new namespace too (Namespace.Util2) later, how can I add methods without modifying the above code
I like the idea of using Singleton mentioned here http://www.adobe./devnet/html5/articles/javascript-design-patterns-pt1-singleton-posite-facade.html:
var Namespace = {
Util: {
util_method1: function() {…},
util_method2: function() {…}
},
Ajax: {
ajax_method: function() {…}
},
some_method: function() {…}
};
Let's say I have to add some methods and new namespace too (Namespace.Util2) later, how can I add methods without modifying the above code
Share Improve this question asked May 24, 2012 at 20:07 Rocky SinghRocky Singh 15.5k31 gold badges106 silver badges146 bronze badges2 Answers
Reset to default 8It is simply:
Namespace.Util.newUtilMethod = function () { };
To add a new namespace,
Namespace.Util2 = { /* definitions */ };
namespace.util.newFunc = function () { };
or, if you're using jquery and want to add a bunch at once:
var newStuff = {
newThing1: function () {...},
newThing2: function () {...},
newThing3: function () {...}
};
$.extend(namespace.util, newStuff);