在 foreach 循环中访问 testval 和 testoption 的最佳方法是什么?这是一个 mootools 草稿。
var some = new Class({
options: { testarray: [1,2,3], testoption: 6 },
initialize: function(options) {
this.testval = '123';
this.options.testarray.each(function(el) {
console.log(this.testval);
console.log(this.options.testoption);
});
}
});
更新: 我可以通过在数组上添加 bind(this) 来修复它,但这是可行的方法吗?
最佳答案
如果我需要从函数中引用多个实例变量,使 this
引用其他内容,我通常会在之前使用 var self = this;
。我发现它比把东西到处装订要好得多; self
变得明确指向实例。
关于javascript - 在每个循环中访问 'this' 对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858290/