开发者

Dynamic Namespace in JavaScript

开发者 https://www.devze.com 2023-01-19 23:59 出处:网络
Is it possible to create a dynamic namespace in a javascript class? I\'m running into an issue w开发者_如何学JAVAhere I need to use this class twice on the same page but it uses the same namespace in

Is it possible to create a dynamic namespace in a javascript class?

I'm running into an issue w开发者_如何学JAVAhere I need to use this class twice on the same page but it uses the same namespace in both spots so it's failing.


you can always instantiate the class like this:

window['stuff'] = new Component()
window['stuff1'] = new Component()

so then you can refer to this instance as window.stuff and window.stuff1

is this what you mean?


var namespace1 = {};
var namespace2 = {};

var myObjMaker = function() {
    this.a = blah;
    this.function = bluh;
}

var makeObjInNS1 =  myObjMaker.apply(namespace1);
var makeObjInNS2 =  myObjMaker.apply(namespace2);
var makeGlobalObj =  myObjMaker.apply(this); 
0

精彩评论

暂无评论...
验证码 换一张
取 消