var self = this?

后端 未结 8 1408
失恋的感觉
失恋的感觉 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:40

    If you are doing ES2015 or doing type script and ES5 then you can use arrow functions in your code and you don't face that error and this refers to your desired scope in your instance.

    this.name = 'test'
    myObject.doSomething(data => {
      console.log(this.name)  // this should print out 'test'
    });
    

提交回复
热议问题