Javascript属性和作用域存在

Javascript属性和作用域存在,javascript,extjs,Javascript,Extjs,我尝试创建以下几个对象: 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'

我尝试创建以下几个对象:

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' );
    }    
  }
}

有人知道解决方案吗?

如果您的
语句中出现以下情况,请尝试将其添加到您的

if ( name in this && this.hasOwnProperty(name) && this[name]) {
   this.fireEvent ( 'Update_on_level_2' );
}
this[name]
我认为更适合IE b/c,我已经了解到它本身在
hasOwnProperty
方面存在一些问题(这可能是IE6的遗留问题,但对于较新版本来说并不是什么大问题)。
this[name]
用于确保您的属性具有值。如果您不关心该值是否为
false
null
,则可以删除该部分。另外,
hasOwnProperty
方法从
原型
中排除属性,这听起来像是您想要的

编辑。
根据@Pointy在下面的评论。

尝试将此添加到您的
语句中,如果
语句:

if ( name in this && this.hasOwnProperty(name) && this[name]) {
   this.fireEvent ( 'Update_on_level_2' );
}
this[name]
我认为更适合IE b/c,我已经了解到它本身在
hasOwnProperty
方面存在一些问题(这可能是IE6的遗留问题,但对于较新版本来说并不是什么大问题)。
this[name]
用于确保您的属性具有值。如果您不关心该值是否为
false
null
,则可以删除该部分。另外,
hasOwnProperty
方法从
原型
中排除属性,这听起来像是您想要的

编辑。
根据@Pointy在下面的评论。

事实上,我发现了代码中的错误:

  • 创建变量时始终声明
    var
  • 调用父方法时,请使用
    ClassName.superclass.methodName.call(this,arg1,arg2..)
    。传递
    this
    非常重要,因为它会将被调用父方法的范围更改为当前对象的范围。(您可以在下面的测试代码中删除
    ,以查看不同的输出)
  • 通常我在使用事件之前在
    initComponent
    中声明
    this.addEvents
    。不确定是否有必要
  • 以下是我的完整测试代码和输出:

    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
    的正确用法实际上,我发现了代码中的错误:

  • 创建变量时始终声明
    var
  • 调用父方法时,请使用
    ClassName.superclass.methodName.call(this,arg1,arg2..)
    。传递
    this
    非常重要,因为它会将被调用父方法的范围更改为当前对象的范围。(您可以在下面的测试代码中删除
    ,以查看不同的输出)
  • 通常我在使用事件之前在
    initComponent
    中声明
    this.addEvents
    。不确定是否有必要
  • 以下是我的完整测试代码和输出:

    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

    的正确用法检查
    此[名称]
    不适用于IE弱点。对象可能具有属性,但该属性的值可能为null。但是,如果属性不是null而是等于数字零、布尔值
    false
    或空字符串,则检查
    此[name]
    也将是
    false
    。因此,它在这里可能是可取的,也可能不是可取的。但是,看起来像是一个开始:var xxx=newobject_level_2({});xxx.setValue('PropA',345678);-fireEvent level_1 xxx.setValue('PropD',345678);-不要使用fireEvent level_2???这些行:
    Object\u level_1.superclass.initComponent.call()
    不应在
    call
    之后有空格。我不确定这是否与这不起作用有关。另外,
    this
    是否范围到正确的对象?检查
    this[name]
    不是针对IE弱点。对象可能具有属性,但该属性的值可能为null。但是,如果属性不是null而是等于数字零、布尔值
    false
    或空字符串,则检查
    此[name]
    也将是
    false
    。因此,它在这里可能是可取的,也可能不是可取的。但是,看起来像是一个开始:var xxx=newobject_level_2({});xxx.setValue('PropA',345678);-fireEvent level_1 xxx.setValue('PropD',345678);-不要使用fireEvent level_2???这些行:
    Object\u level_1.superclass.initComponent.call()
    不应在
    call
    之后有空格。我不确定这是否与这不起作用有关。另外,
    这是否是正确对象的作用域?var Obj1,var Obj2将声明变量-我跳过var的原因是我想声明对象(var Obj1=新Obj1(Obj1是var)?)-分离设置值的原因,整个问题是确保设置值将以正确的方法发生。。。请尝试阅读一个问题。对不起,可能您的问题不清楚,这就是为什么答案可能不符合您的要求。@bensiu这可能会引起您对
    var
    var Obj1,var Obj2将声明varables的兴趣-我跳过要声明对象的var的原因(var Obj1=new Obj1(Obj1是var)?)-将设定值和整个问题分开的原因是为了确保设定值以正确的方法发生。。。试着读一个问题。对不起,也许你的问题不清楚,这就是为什么答案可能不符合你的要求。@bensiu关于
    var