我嘗試創建一個像這樣的一些對象: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