blo*_*ong 3 javascript oop design-patterns extjs3
免责声明:我使用的是ExtJS 3,但除了常用的命名空间功能之外,我认为这与问题无关.
我有一个在一个非常长的命名空间中声明的单例,如下所示:
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();
Run Code Online (Sandbox Code Playgroud)
我通过像REALLY.REALLY.LONG.NAMESPACE.Singleton.otherFunction();和的调用在其他模块中使用它REALLY.REALLY.LONG.NAMESPACE.Singleton.publicFunction(myObject);.我想知道我是否可以通过设置具有单例别名的客户端模块来交换这些调用,即var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;,以便我可以调用singleton.otherFunction();.我想知道这是一个反模式,还是有任何陷阱(内存?)我可能会遇到这种用法.
谢谢StackOverflow!
我想知道我是否可以通过设置具有单例别名的客户端模块来交换这些调用
是的你可以.
我想知道这是一个反模式,还是有任何陷阱(内存?)我可能会遇到这种用法.
不,没有任何我能想到的,它比调用完全限定版本更快.
例:
function somefunc(){
var singleton = REALLY.REALLY.LONG.NAMESPACE.Singleton;
singleton.publicFunction();
};
Run Code Online (Sandbox Code Playgroud)
要么:
(function somfunc(singleton){
}(REALLY.REALLY.LONG.NAMESPACE.Singleton));
Run Code Online (Sandbox Code Playgroud)
检测结果: