【问题标题】:How to correctly reference "this"? [duplicate]如何正确引用“this”? [复制]
【发布时间】:2013-04-25 13:24:44
【问题描述】:

假设我有以下:

var object = {
    myfunc: function() {
        $.ajax({
           url: url,
           format: format,
           success: function() {

           console.log(this) // This refers to the ajax call and not "object"

                $.ajax({
                  url: url,
                  format: format,
                  success: function() {
                    console.log(this) // this refers to nested ajax call and not "object"
                  }
                });


           }
        });
    }
}

如何让“this”引用“object”而不是 ajax 调用?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    使用$.proxy() 将自定义上下文传递给回调函数

    var object = {
        myvar : "hello",
        myfunc : function() {
            $.ajax({
                url : url,
                format : format,
                success : $.proxy(function() {
    
                    console.log(this) // This refers to the ajax
                    // call and
                    // not "object"
    
                    $.ajax({
                        url : url,
                        format : format,
                        success : function() {
                            console.log(this) // this
                            // refers to
                            // nested ajax call
                            // and not "object"
                        }
                    });
    
                }, this)
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      当您仍在 this 保存您想要的值的上下文中时,将 this 的值复制到另一个变量。

      var object = {
          myfunc: function() {
              var myObject = this;
              $.ajax({
      

      然后使用该变量(它将在其中声明的函数的范围内,除非它们用另一个同名变量屏蔽它)。

      success: function() {
          console.log(myObject);
      }
      

      【讨论】:

        【解决方案3】:

        在我看来,这是一种比另一种更容易的方法。只需将引用存储在局部变量中并使用它而不是 this

        var object = {
            var thisReference = this;
            myfunc: function() {
                $.ajax({
                   url: url,
                   format: format,
                   success: function() {
        
                   console.log(thisReference ) 
        
                        $.ajax({
                          url: url,
                          format: format,
                          success: function() {
                            console.log(thisReference ) 
                          }
                        });
        
        
                   }
                });
            }
        }
        

        【讨论】:

        • 这不是语法有效的 Javascript。
        【解决方案4】:

        使对象成为构造函数。

        /*
          constructors start with capital letters, by convention.  
          Javascript already has an 'Object' constructor, so we'll change the name.
        */
        var MyObject = function(){
          // 'this' now refers to the object.  
          // We'll save a reference to it for use within functions
          var me = this;
        
          this.myvar: "hello";
          this.myfunc: function(){
            // do whatever you want.  Use 'me' to access your object.
            console.log(me);  // the object!
          }
        }
        

        你使用它的方式可能会改变,这取决于你是否想假装是面向对象的。就是这样:

        var obj = new MyObject();  //make the object
        obj.myfunc();            //call your function
        

        【讨论】:

          猜你喜欢
          • 2015-12-07
          • 2012-02-27
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 2016-03-09
          • 2014-03-31
          • 1970-01-01
          相关资源
          最近更新 更多