2011-01-20 28 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