【问题标题】:'this' is undefined inside an Object in javascript accessing property of object by another property'this'在javascript中的对象内部未定义,通过另一个属性访问对象的属性
【发布时间】:2011-05-19 10:27:21
【问题描述】:
Type= {
        Container: $get('ctl00_Main_rbtnlst_Type'),
        Local: this.Container.getElementsByTagName('input'),
        Foreign:this.Container.getElementsByTagName('input')
    }

当我在 firebug 控制台中运行此代码时,我收到错误“this.Container”未定义,即使它已定义。我还能如何访问 Local 和 Foreign 属性中的 Container 属性。我什至试过这个。

Type= {
        Container: $get('ctl00_Main_rbtnlst_Type'),
        Local: Container.getElementsByTagName('input'),
        Foreign:Container.getElementsByTagName('input')
    }

【问题讨论】:

    标签: javascript asp.net class this prototype-programming


    【解决方案1】:

    在实例化时你不能得到this。你可以这样做:

    Type= {
            Container: $get('ctl00_Main_rbtnlst_Type'),
            Local: function(){return this.Container.getElementsByTagName('input');},
            Foreign: function(){return this.Container.getElementsByTagName('input');}
        }
    

    稍后使用Type.Local()/Type.Foreign()获取本地或外国

    如果您在实例中需要本地/外部,则使用此冗余模式:

    Type= {
                Container: $get('ctl00_Main_rbtnlst_Type'),
                Local: $get('ctl00_Main_rbtnlst_Type')
                         .getElementsByTagName('input');},
                Foreign: $get('ctl00_Main_rbtnlst_Type')
                          .getElementsByTagName('input');}
            }
    

    或者使用这个立即执行的函数:

    var Type = (function(){
       var container = $get('ctl00_Main_rbtnlst_Type'),
           local = container.getElementsByTagName('input'),
           foreign = container.getElementsByTagName('input');
       return {
               Container: container,
               Local: local,
               Foreign: foreign
              }
    })();
    

    为了完整起见,您还可以使用一些getters,但这不适用于所有浏览器(尤其是在 IE 中

    var Type = {
        Container: $get('ctl00_Main_rbtnlst_Type'),
        get Local() {return this.Container.getElementsByTagName('input');},
        get Foreign() {return this.Container.getElementsByTagName('input');}
    }
    

    注意LocalForeign 是一样的,这是你想要的吗?

    【讨论】:

    • @Kooilnc 匿名函数是唯一的方法??我怎么能处理一个属性
    • @Kooilnc 现在为什么我没有想到关闭感谢标记为答案的提醒顺便说一句 ctl00_Main_rbtnlst_Type 是一张桌子,里面有 2 个单选按钮,因此本地和外国将指两个不同的控制。谢谢
    【解决方案2】:

    你可以这样做:

    var container = $get('ctl00_Main_rbtnlst_Type');
    Type = {
        Container: container,
        Local: container.getElementsByTagName('input'),
        Foreign: container.getElementsByTagName('input')
    }
    

    但你可能想要的是这个:

    function Type(containerId){
        var container = $get(containerId);
        return {
            Container: container,
            Local: container.getElementsByTagName('input'),
            Foreign: container.getElementsByTagName('input')
        }
    }
    
    var obj = Type('ctl00_Main_rbtnlst_Type');
    // can now use obj.Container, obj.Local and obj.Foreign
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多