【问题标题】:How To Pass "this" to a Function in a Nested .each() loop within a .getJSON() method如何在 .getJSON() 方法中的嵌套 .each() 循环中将“this”传递给函数
【发布时间】:2012-05-22 10:26:34
【问题描述】:

所以我知道问题出在哪里,但我不知道解决问题的正确方法(语法)。

如果您从“myFunction”中获取代码并将其放在我调用“myFunction”的 .each 循环中,那么我的代码可以正常工作,因为“this”正在被定义并且在我的 JSON 对象的范围内参考。

但是,当我编写函数并在 JSON 对象的范围之外声明它时,它不再知道“this”是什么并返回“undefined”值。

如何将“this”作为参数传递给我的函数,以便正确获取值?

    $.getJSON('labOrders.json', function(json) {
    var aceInhibitors = "",
        antianginal = "",
        anticoagulants = "",
        betaBlocker = "",
        diuretic = "",
        mineral = "",
        myFunction = function () {
            aceInhibitors += "<div class='row drugLineItem'>",
            aceInhibitors += "<div class='columns moneydot' style='background:none;'></div>",
            aceInhibitors += "<div class='columns drugTitleWrap'>",
            aceInhibitors += "<div class='row drugTitle'>"+this.name+"</div>",
            aceInhibitors += "<div class='row drugDose'>"+this.strength+"</div>",
            aceInhibitors += "<div class='columns'></div>",
            aceInhibitors += "<div class='columns orderDeets dxRefill'>"+this.refills+"</div>",
            aceInhibitors += "<div class='columns orderDeets dxPillCount'>"+this.pillCount+"</div>",
            aceInhibitors += "<div class='columns orderDeets dxSig'>"+this.sig+"</div>",
            aceInhibitors += "<div class='columns orderDeets dxRoute'>"+this.route+"</div>",
            aceInhibitors += "<div class='columns orderDeets drugTab'>"+this.dose+"</div>"
        }

  $.each(json.medications, function(index, orders) {
    $.each(this.aceInhibitors, function() {
        myFunction();

    });
    $.each(this.antianginal, function() {

    });
  });

  $('#loadMedSections').append(aceInhibitors);



});

【问题讨论】:

    标签: jquery json scope this each


    【解决方案1】:

    你有两个选择:

     $.each(this.aceInhibitors, myFunction);
    

    不需要包装函数,它会有正确的上下文。

    第二个选项:

    $.each(this.aceInhibitors, function() {
        myFunction.call(this);
    });
    

    这将调用它的上下文包装函数的上下文。

    【讨论】:

    • 谢谢!调用方法有效!计时器完成后会检查!
    • 除非你出于某种原因需要包装函数,否则我会删除它。提出的第一个选项更简洁且功能等效。很高兴它可以工作。
    • 这就是我所说的使用函数来执行不同函数的唯一目的的习惯用法。基本上,我认为:function() { f(); } 函数 f 的包装函数。对于var w = function() { f(); };w() 基本上等同于f(),除了上下文(this)将是w 的上下文,而不是实际的调用者。 (w 的this 将成为w 的调用者,而fthis 将基本上引用w。基本上,您将f 的执行无缘无故地包装在w 中,因此隐藏执行的实际上下文。
    • 啊....明白了....但是如果没有包装函数,我无法让它工作...通过将命名函数作为参数包含在每个函数中,调用的是什么开火的功能??或者我是否需要删除函数首先触发的包装函数语法?
    • 我是对的....我必须删除包装功能才能使您的第一个解决方案起作用。非常感谢!我很感激聊天!
    【解决方案2】:

    有一个名为call 的函数,它允许您为this 设置一个显式值。

    例如,如果您执行以下操作:

    myFunction.call(someobject);
    

    然后myFunction 将被调用this = someobject

    在您的情况下,您可能可以这样做:

    $.each(this.aceInhibitors, function() {
      myFunction.call(this); // will use the current value of "this" inside myFunction as well.
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2016-02-27
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多