var self = this?

后端 未结 8 1423
失恋的感觉
失恋的感觉 2020-11-22 09:10

Using instance methods as callbacks for event handlers changes the scope of this from \"My instance\" to \"Whatever just called the callback\"

8条回答
  •  情话喂你
    2020-11-22 09:45

    var functionX = function() {
      var self = this;
      var functionY = function(y) {
        // If we call "this" in here, we get a reference to functionY,
        // but if we call "self" (defined earlier), we get a reference to function X.
      }
    }
    

    edit: in spite of, nested functions within an object takes on the global window object rather than the surrounding object.

提交回复
热议问题