Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending existing singleton

I like the idea of using Singleton mentioned here http://www.adobe.com/devnet/html5/articles/javascript-design-patterns-pt1-singleton-composite-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

like image 824
Rocky Singh Avatar asked Jun 09 '26 00:06

Rocky Singh


2 Answers

It is simply:

Namespace.Util.newUtilMethod = function () { };

To add a new namespace,

Namespace.Util2 = { /* definitions */ };
like image 193
Daniel A. White Avatar answered Jun 10 '26 13:06

Daniel A. White


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);
like image 21
jbabey Avatar answered Jun 10 '26 13:06

jbabey