【问题标题】:Call function in parent's parent from inside jquery's .each从 jquery 的 .each 内部调用父级父级中的函数
【发布时间】:2012-11-07 08:28:57
【问题描述】:

我在 javascript 中创建了一个名为 QuoteProductService() 的“类”,见下文。 我在原型中添加了两个函数,现在,我试图从另一个函数 (getFakeQuoteProducts) 内的 jquery $.each 中调用其中一个函数 (getQuoteProductFromArray)。这行不通。我尝试添加“this.”,但这也不起作用,因为 .each 中的“this”指的是循环中的当前元素。

我应该怎么做?

function QuoteProductService() {

}

QuoteProductService.prototype.getQuoteProductFromArray =  function(quoteproductarray, quoteproductid){
     var founditem=null;
     // do stuff
    return founditem;
}

QuoteProductService.prototype.getFakeQuoteProducts = function(){
    // do something to fill the mappedQuoteProducts array
    $.each(mappedQuoteProducts, function (index, quoteproduct) {
        if (quoteproduct!=-null) {
            if (quoteproduct.parentid != "") {
                // this is where it goes wrong :
                var parent = getQuoteProductFromArray(mappedQuoteProducts, quoteproduct.parentid);
                if (parent != null) {
                    parent.attachChild(quoteproduct);
                }
            }
        }
    });
}

【问题讨论】:

    标签: javascript jquery scope each


    【解决方案1】:

    首先,您提供了错误的方法名称 - getQuoteProductFromFlatArray 而不是 getQuoteProductFromArray。其次,在 JS 中,您必须为实例方法提供范围。 实现这一点的最简单方法是将this 引用存储到其他一些私有变量中。请参阅下面的示例。

    function QuoteProductService() {
    
    }
    
    QuoteProductService.prototype.getQuoteProductFromArray =  function(quoteproductarray, quoteproductid){
         var founditem=null;
         // do stuff
        return founditem;
    }
    
    QuoteProductService.prototype.getFakeQuoteProducts = function(){
        var me = this; // store this into me
    
        // do something to fill the mappedQuoteProducts array
        $.each(mappedQuoteProducts, function (index, quoteproduct) {
            // this === me will return false
            if (quoteproduct!=-null) {
                if (quoteproduct.parentid != "") {
                    // this is where it goes wrong :
                    var parent = me.getQuoteProductFromArray(mappedQuoteProducts, quoteproduct.parentid);
                    if (parent != null) {
                        parent.attachChild(quoteproduct);
                    }
                }
            }
        });
    }
    

    【讨论】:

    • 太棒了!像魅力一样工作:) 非常感谢
    【解决方案2】:

    var self = this; 添加到getFakeQuoteProducts 函数的开头。然后像这样调用getQuoteProductFromFlatArrayself.getQuoteProductFromFlatArray

    【讨论】:

    • 太棒了!像魅力一样工作:) 非常感谢
    【解决方案3】:

    在调用 each 之前保存对您的 QuoteProductService 实例的引用

    QuoteProductService.prototype.getFakeQuoteProducts = function(){
      var _this = this;
      // do something to fill the mappedQuoteProducts array
      $.each(mappedQuoteProducts, function (index, quoteproduct) {
          if (quoteproduct!=-null) {
              if (quoteproduct.parentid != "") {
                  // this is where it goes wrong :
                  var parent = _this.getQuoteProductFromFlatArray(mappedQuoteProducts, quoteproduct.parentid);
                  if (parent != null) {
                      parent.attachChild(quoteproduct);
                  }
              }
           }
        });
    }
    

    【讨论】:

    • 将此变量命名为thatself 也很常见。
    • self 是保留字 (afaik),用于被浏览器引用 window 对象。 more information at mdn。除此之外,我认为这是一个品味问题。我个人不喜欢that
    • 太棒了!像魅力一样工作:) 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多