「这个」在 JavaScript 中,通过另一个属性访问对象的属性时,对象内部未定义

发布于 2024-11-08 13:19:34 字数 510 浏览 0 评论 0原文

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

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

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

when i ran through this code inside firebug console i get error 'this.Container' is undefined even though it is defined. How else can i access the Container property inside the Local and Foreign property. I even tried this.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-11-15 13:19:34

实例化时无法获取 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
          }
})();

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

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

注意本地国外都是一样的,是你想要的吗?

You can't get this while instantiating. You can do:

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

And later on get Local or Foreign using Type.Local()/Type.Foreign()

or use this reduntant pattern if you need Local/Foreign within the instance:

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

Or use this immediately executed function:

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

and to be complete, you can also use a few getters, but that won't work in all browsers (especially not in IE<9)

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

note: Local and Foreign are the same, is that what you intended?

耳根太软 2024-11-15 13:19:34

你可以这样做:

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

You can do this:

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

But what you probably want is this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文