【问题标题】:In jQuery, how do you resolve the scope of "this", when you are in the scope of each()?在 jQuery 中,当您在 each() 的范围内时,如何解析“this”的范围?
【发布时间】:2010-02-18 19:00:21
【问题描述】:

我创建了一个对象,并在对象中有一个方法 setup()。

this.debug = function (){...}

this.setup = function(){    

  var fieldsets = form.children("fieldset");

  fieldsets.each(function(){        
    this.debug($(this).attr("class")));
  });


}

我正在尝试调用在 Object 范围内但不在每个范围内的 this.debug,因为这是一个不同的 this...

如何访问 this.debug?

【问题讨论】:

    标签: javascript jquery scope this


    【解决方案1】:

    在 this.debug 之后说var that = this,然后说that.debug

    【讨论】:

    • 这个那个... that.funny = 101
    • 这应该可以。将 that = this 放在 setup() 方法的开头。 (通过工作,我的意思是正确调用 debug() 方法,它仍然可能抛出错误。)
    • 你真的很想做var that = this。一般来说,省略 var 是个坏主意。
    • 谢谢@Kyle - 我上次写 JavaScript 已经有一段时间了:P 你忘了这些小细节......
    【解决方案2】:

    这基本上是 Skilldrik 的答案,但会向您展示它在哪里最有效

    this.setup = function(){    
      // save it in a scoped var...  some people use "self" for this purpose, i prefer
      // naming it whatever the outer object actually is...
      var containingObject = this;
    
      var fieldsets = form.children("fieldset");
    
      fieldsets.each(function(){        
        // use that scoped var later!
        containingObject.debug($(this).attr("class")));
      });      
    }
    

    【讨论】:

    • +1。此外,如果您的调试方法不使用实例数据,您可以在静态上下文中调用它: ContainingObject.debug($(this).attr("class")));.
    【解决方案3】:

    在 jQuery 1.4 中你可以这样做:

    this.debug = function (){...}
    
    this.setup = function(){    
    
      var fieldsets = form.children("fieldset");
    
      fieldsets.each(jQuery.proxy(function(){
        this.debug($(this).attr("class")));
      },this);
    }
    

    jQuery.proxy(function, object) 函数将采用 2 个参数:

    • 该函数将是循环中使用的函数。
    • 对象参数将是函数内的this 对象。

    通过这种方式,您可以将thiseach函数内部的外部范围转移。

    【讨论】:

    • 对于将之前的 for 循环转换为 jQuery.each 函数非常有用;如果在每个匿名函数中定义另一个依赖于外部this 的匿名函数,var that = this; 技巧就不能正常工作。在调用内部函数之前,局部变量that 将消失。
    【解决方案4】:

    我在 Greasemonkey-Console 中试过这个:

    this.debug = function() {
        console.log("foo");
    };
    
    this.setup = function() {
    
        var fieldsets = form.children("fieldset");
    
        fieldsets.each(function(){
            debug($(this).attr("class"));
        });
    };
    

    这将搜索任何调试的范围......希望是上面的功能。如果您分配具有相同名称的变量,这将失败:)

    【讨论】:

    • thisforeach 循环中将包含当前迭代的字段集对象。
    【解决方案5】:

    我通常这样做:(注意:下面的示例来自记忆,但看起来不错):

    function CustomObject()
    {
      var _instance = this;
    
      this.debug = function(){...};
      this.setup =
        function()
        {    
          var fieldsets = form.children("fieldset");
          fieldsets.each(
            function()
            {        
              _instance.debug($(this).attr("class"));
            });
        };
    }
    

    【讨论】:

      【解决方案6】:
      this.debug = function (){...}
      
      this.setup = function(){    
        var that = this;
        var fieldsets = form.children("fieldset");
      
        fieldsets.each(function(){        
          that.debug($(this).attr("class")));
        });
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-26
        • 2012-08-26
        • 2011-11-29
        • 2011-07-17
        • 2021-02-01
        • 2015-09-11
        • 2010-09-09
        • 2020-12-23
        相关资源
        最近更新 更多