[英]Aliasing a function object in JavaScript
Disclaimer: I am using ExtJS 3, but I don't think it's very relevant to the question, outside of the common use of it's namespacing function. 免责声明:我使用的是ExtJS 3,但除了常用的命名空间功能之外,我认为这与问题无关。
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();
我通过
REALLY.REALLY.LONG.NAMESPACE.Singleton.otherFunction();
等调用在其他模块中使用它REALLY.REALLY.LONG.NAMESPACE.Singleton.otherFunction();
and REALLY.REALLY.LONG.NAMESPACE.Singleton.publicFunction(myObject);
和
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, ie
var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;
我想知道我是否可以通过设置带有单例别名的客户端模块来交换这些调用,即
var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;
, so that I can call singleton.otherFunction();
,这样我就可以调用
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! 谢谢StackOverflow!
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. 不,没有任何我能想到的,它比调用完全限定版本更快。
Example: 例:
function somefunc(){
var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;
singleton.publicFunction();
};
Or: 要么:
(function somfunc(singleton){
}(REALLY.REALLY.LONG.NAMESPACE.Singleton));
Test Results: 检测结果:
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. 我并没有真正看到这里的蚂蚁模式,相反,我认为有机会让自己更简单,从开发人员的角度来看可能更容易管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.