2011-01-20 24 views
0

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

是否有人知道解決的辦法?

回答

1

嘗試添加這對您的if聲明:

if (name in this && this.hasOwnProperty(name) && this[name]) { 
    this.fireEvent ('Update_on_level_2'); 
} 

this[name]我相信更多的IE B/C我已閱讀,它有一些問題, hasOwnProperty自身(這可能是一個遺留但是對於IE6而言,並不會成爲新版本的問題)。 this[name]是爲了確保你的財產有價值。如果您不在意價值是falsenull,那麼可以取出該部分。此外,hasOwnProperty方法排除prototype的屬性,這聽起來像你要去的東西。

編輯。 根據@ Pointy的評論如下。

+0

「this [name]」的檢查不適用於IE弱點。對象有可能擁有一個屬性,但該屬性的值可能爲空。然而,如果該屬性不是null,而是等於數字零,布爾型「false」值或空字符串,那麼檢查`this [name]`也將是`false'。因此這裏可能會或可能不需要。 – Pointy 2011-01-20 14:24:42

+0

似乎有點像開始: 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

2

其實,我在代碼中發現的錯誤:

  1. 創建變量
  2. 當調用父的方法,使用ClassName.superclass.methodName.call(this, arg1, arg2..)時,一定要申報var。通過this是很重要的,因爲它會將被調用父方法的範圍更改爲當前對象的範圍。 (您可以在我的下列測試代碼中刪除this以查看不同的輸出)。
  3. 通常我在使用事件之前聲明this.addEventsinitComponent。不知道是否有必要。

這裏是我完整的測試代碼與輸出:

var Obj1 = Ext.extend(Ext.util.Observable, { 
    propA: null, 
    propB: null, 
    initComponent: function() { 
     Obj1.superclass.initComponent.call(this); 
    }, 
    setValue: function(name, value) { 
     if (name in this) { //Used dtan suggestion 
      this[name] = value; 
      console.log(this.propA, this.propB, this.propC, this.propD); 
     }else{ 
      console.log(name+" is not in Obj1"); 
     } 
    } 
}); 

var Obj2 = Ext.extend(Obj1, { 
    propC: null, 
    propD: null, 
    initComponent: function() { 
     Obj2.superclass.initComponent.call(this); 
    }, 
    setValue: function(name, value) { 
     Obj2.superclass.setValue.call(this, name, value); 
    } 
}); 

var obj1 = new Obj1(); 
var obj2 = new Obj2(); 
obj1.setValue('propA', '1a'); //1a null undefined undefined 
obj1.setValue('propB', '1b'); //1a 1b undefined undefined 
obj2.setValue('propC', '2c'); //null null 2c null 
obj2.setValue('propD', '2d'); //null null 2c 2d 
obj1.setValue('propA', '1a'); //1a 1b undefined undefined 
obj1.setValue('propB', '1b'); //1a 1b undefined undefined 
obj1.setValue('propC', '1c'); //propC is not in Obj1 
obj1.setValue('propD', '1d'); //propD is not in Obj1 
obj2.setValue('propA', '2a'); //2a null 2c 2d 
obj2.setValue('propB', '2b'); //2a 2b 2c 2d 
obj2.setValue('propC', '2c'); //2a 2b 2c 2d 
obj2.setValue('propD', '2d'); //2a 2b 2c 2d 

嘗試讀取ExtJS的開發人員如何編寫自己的代碼在src文件夾。您將看到正確的用法Ext.extend