最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extending existing singleton - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

It 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);
发布评论

评论列表(0)

  1. 暂无评论