简体   繁体   English

为什么此变量引用不起作用?

[英]Why does this variable reference not work?

this.prefixMonday[0] exists at the current scope this.prefixMonday is an array of three checkboxes this is within the initComponent method of an extend of a panel this.prefixMonday [0]在当前作用域中存在this.prefixMonday是三个复选框的数组,它位于面板扩展的initComponent方法中

this.weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

for(var i = 0; i<7; i++){
   this['prefix' +this.weekdays[i] +'[0]'].on('check',this.someFunction, this);
}

Firebug says it can't find: this['prefix' +this.weekdays[i] +'[0]'] Firebug说找不到:this ['prefix'+ this.weekdays [i] +'[0]']

I'm pretty sure you have to acces 我很确定你必须加入

this['prefix' +this.weekdays[i]][0]

Otherwise JavaScript will search for a key with exactly the string 'prefixMonday[0]' and I don't think this is what you want. 否则,JavaScript会使用完全字符串'prefixMonday [0]'搜索键,我认为这不是您想要的。 To make this more readable you might want to use a helper variable to store the name: 为了使它更具可读性,您可能需要使用一个辅助变量来存储名称:

for(var i = 0; i<7; i++){
    var key = 'prefix' +this.weekdays[i];
    this[key][0].on('check',this.someFunction, this);
}

this.prefixMonday[0] is not equivalent to this['prefixMonday[0]'] . this.prefixMonday[0]不等于this['prefixMonday[0]'] It would be equivalent to this['prefixMonday'][0] . 等效于this['prefixMonday'][0] Try 尝试

for(var i = 0; i<7; i++){
   this['prefix' +this.weekdays[i]][0].on('check',this.someFunction, this);
}
this['prefix' +this.weekdays[i] +'[0]']

This would create something that looks like 这将创建看起来像

this['prefixWednesday[0]']

Note that the array reference is within the string, so you're looking for a key that contains '[0]' as literal text. 请注意,数组引用位于字符串内,因此您正在寻找包含'[0]'作为原义文本的键。 Are you sure you wouldn't awnt something more like: 您确定不会再显示以下内容:

this['prefixWednesday'][0]...

instead? 代替?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM