Extending an 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 need to add some methods and a new namespace (Namespace.Util2) later, how can I add methods without changing the above code

+3
source share
2 answers

It's simple:

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

To add a new namespace,

Namespace.Util2 = { /* definitions */ };
+8
source
namespace.util.newFunc = function () { }; 

or, if you use jquery and want to add a bunch at once:

var newStuff = {
    newThing1: function () {...},
    newThing2: function () {...},
    newThing3: function () {...}
};

$.extend(namespace.util, newStuff);
+3
source