1
哪种方法可以从JavaScript中的另一个名称空间访问函数或属性?例如:JavaScript名称空间
var NS = {};
NS.A = {
prop1: 'hello',
prop2: 'there',
func: function() {alert('boo');}
};
NS.B.C = {
func1: function() {
// Here I want to access the properties and function from the namespace above
alert(NS.A.prop1 + NS.A.prop2); // ?
NS.A.func(); // ?
}
};
NS.B.C.func1();
好吧,但是我必须为每个要访问的属性或函数写入100次NS.A吗?或者我应该更好地创建一个指向NS.A的局部变量?事实上,我正在尝试第二种,但也不觉得它很干净。 – user287966 2011-04-17 16:54:05
你应该阅读关于JavaScript关闭。在NS.A之外有一个局部变量指向外部,肯定比在变量外部引用一个变量更快。 – Mickel 2011-04-17 17:03:53