javascript - 循环遍历EXTJS中的数组时如何获取范围
问题描述
我有一个数组,我需要遍历它以获得一个值。
someFunc: function(item) {
var array = Ext.select(".item").elements;
var elements;
var itemId = item.id;
Ext.each(array, function(ob){
if (ob.id === itemId) {
elements = ob.getElementsByTagName("input");
Ext.each(elements, function(att){
if (att.getAttribute("name") === "filter") {
console.log(att);
this.callFunction();
// here I want to call a function to do some stuff
// but `"this"` here refers to input element
//I should be able to call the function outside the someFunc, but I'm loosing scope
}
});
}
});
},
callFunctionL function() {
}
如何在遍历数组数组时保持范围?
解决方案
有2种方式:
a) 将scope
参数传递给每个:
Ext.each(o, function() {
console.log(this);
}, this);
b) 在闭包中捕获范围:
var self = this;
Ext.each(o, function() {
console.log(self);
});
推荐阅读
- git - 在单个比较窗口中比较多个文件的差异
- cakephp - Cakephp - 禁用 CSRF 以在 android 应用程序中使用 api
- python-3.x - 在发送到 SageMaker TF Serving 之前压缩图像
- r - 对于每个主题多行的数据,将纵向值重塑为宽值
- c# - Facebook 密码更改,使用 C#
- java - JDBC 模板运行缓慢
- visual-studio - VS 2017 - NuGet Manager 不添加引用
- python - Numpy数组有头尾方法吗?
- node.js - 为什么 formData 不适用于多个文件?
- mysql - 组合多个表生成视图