oop - Aliasing a function object in JavaScript - Stack Overflow

Disclaimer: I am using ExtJS 3, but I don't think it's very relevant to the question, outside

Disclaimer: I am using ExtJS 3, but I don't think it's very relevant to the question, outside of the mon use of it's namespacing function.

I have a singleton that's declared in a really long namespace like this:

Ext.ns("REALLY.REALLY.LONG.NAMESPACE");

var Singleton = (function() {   
    var foo = {
        bar: "baz"
    }; 

    var privateFunction = function(param){
        // ...
        return foo;
    };

    var single = Ext.extend(Object, {
        constructor: function(config) {
            Ext.apply(this, config);
        },
        otherFunction: privateFunction,     
        publicFunction: function (someObject){
            // do stuff with someObject
        }
    });

    return single;

})();
// Make it a singleton
REALLY.REALLY.LONG.NAMESPACE.Singleton = new Singleton();

I use it in other modules via calls like REALLY.REALLY.LONG.NAMESPACE.Singleton.otherFunction(); and REALLY.REALLY.LONG.NAMESPACE.Singleton.publicFunction(myObject); . I'm wondering if I can swap out those calls by setting up the client module with an alias to the singleton, i.e. var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton; , so that I can call singleton.otherFunction();. I'm wondering if this is an anti-pattern , or if there are any pitfalls (memory?) I might run into through this usage.

Thanks StackOverflow!

Disclaimer: I am using ExtJS 3, but I don't think it's very relevant to the question, outside of the mon use of it's namespacing function.

I have a singleton that's declared in a really long namespace like this:

Ext.ns("REALLY.REALLY.LONG.NAMESPACE");

var Singleton = (function() {   
    var foo = {
        bar: "baz"
    }; 

    var privateFunction = function(param){
        // ...
        return foo;
    };

    var single = Ext.extend(Object, {
        constructor: function(config) {
            Ext.apply(this, config);
        },
        otherFunction: privateFunction,     
        publicFunction: function (someObject){
            // do stuff with someObject
        }
    });

    return single;

})();
// Make it a singleton
REALLY.REALLY.LONG.NAMESPACE.Singleton = new Singleton();

I use it in other modules via calls like REALLY.REALLY.LONG.NAMESPACE.Singleton.otherFunction(); and REALLY.REALLY.LONG.NAMESPACE.Singleton.publicFunction(myObject); . I'm wondering if I can swap out those calls by setting up the client module with an alias to the singleton, i.e. var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton; , so that I can call singleton.otherFunction();. I'm wondering if this is an anti-pattern , or if there are any pitfalls (memory?) I might run into through this usage.

Thanks StackOverflow!

Share Improve this question edited Jun 26, 2012 at 20:43 Niko 26.7k9 gold badges96 silver badges112 bronze badges asked Jun 26, 2012 at 19:28 blongblong 2,7138 gold badges47 silver badges114 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I'm wondering if I can swap out those calls by setting up the client module with an alias to the singleton

Yes, you can.

I'm wondering if this is an anti-pattern , or if there are any pitfalls (memory?) I might run into through this usage.

No, there aren't any that I can think of and it is faster than calling the fully-qualified version.

Local Alias Pattern


Example:

function somefunc(){
    var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;

    singleton.publicFunction();
};

Or:

(function somfunc(singleton){

}(REALLY.REALLY.LONG.NAMESPACE.Singleton));

Test Results:

http://jsfiddle/jMg9A/

There is no issue with creating a reference to the original "object". In many cases we create a namespace to organize our code, but of course, this can lead to really long namespaces that we really don't wish to reference later, thus creating a local reference to that namespace is an excellent idea so that you can change it in one place instead of various places.

I don't really see an ant-pattern here, instead I see an opportunity to make it simpler for yourself and probably a little more manageable from a developer standpoint.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742335555a4424576.html

相关推荐

  • oop - Aliasing a function object in JavaScript - Stack Overflow

    Disclaimer: I am using ExtJS 3, but I don't think it's very relevant to the question, outside

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信