我尝试创建一个像这样的一些对象:JavaScript属性存在和范围
Object_level_1 = Ext.extend (Ext.util.Observable, {
PropA: null,
ProbB: null,
initComponent: function() {
Object_level_1.superclass.initComponent.call();
},
setValue: function (name, value) { // it will come as 'PropA', 45
if (this.hasOwnProperty (name)) { // ' fixed base on dtan answer
// here is a problem 1
// how I can access correct property and set it up
// problem 2
// How I set up property value of right property by having variable name
this.fireEvent ('Update_on_level_1');
}
}
}
Object_level_2 = Ext.extend (Object_level_1, {
PropC: null,
ProbD: null,
initComponent: function() {
Object_level_1.superclass.initComponent.call();
},
setValue: function (name, value) { // it will come as 'PropA', 45 or 'PropC', 100
Object_level_2.superclass.setValue (name, value);
if (this.hasOwnProperty (name)) { // ' fixed base on dtan answer
// here is a problem 1 again
// how I can access correct property and set it up
// problem 2 again
// How I set up property value of right property by having variable name
this.fireEvent ('Update_on_level_2');
}
}
}
是否有人知道解决的办法?
“this [name]”的检查不适用于IE弱点。对象有可能拥有一个属性,但该属性的值可能为空。然而,如果该属性不是null,而是等于数字零,布尔型“false”值或空字符串,那么检查`this [name]`也将是`false'。因此这里可能会或可能不需要。 – Pointy 2011-01-20 14:24:42
似乎有点像开始: var xxx = new Object_level_2({}); xxx.setValue('PropA',345678); - fireEvent level_1 xxx.setValue('PropD',345678); - 不要fireEvent level_2 ??? – bensiu 2011-01-20 15:31:12