gpt4 book ai didi

javascript - 在 EXTJS 中循环遍历数组时如何获取范围

转载 作者:行者123 更新时间:2023-12-02 08:07:42 32 4
gpt4 key购买 nike

我有一个数组,我需要遍历它来获取一个值。

    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() {

}

如何在遍历数组的数组时保持作用域?

最佳答案

有两种方式:

a) 将 scope 参数传递给每个:

Ext.each(o, function() {
console.log(this);
}, this);

b) 在闭包中捕获作用域:

var self = this;
Ext.each(o, function() {
console.log(self);
});

关于javascript - 在 EXTJS 中循环遍历数组时如何获取范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070277/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com