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

问题描述 投票:2回答:1

我有一个数组,我需要迭代来获取一个值。

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

}

如何在迭代数组数组时保持范围?

javascript extjs
1个回答
6
投票

有两种方式:

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

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

b)在闭包中捕获范围:

var self = this;
Ext.each(o, function() {
    console.log(self);
});
© www.soinside.com 2019 - 2024. All rights reserved.