'this' is undefined in JavaScript class methods

前端 未结 5 1063
悲&欢浪女
悲&欢浪女 2020-12-13 11:51

I\'m new to JavaScript. New as far as all I\'ve really done with it is tweaked existing code and wrote small bits of jQuery.

Now I\'m attempting to write a \"class\"

5条回答
  •  隐瞒了意图╮
    2020-12-13 12:27

    I just wanted to point out that sometimes this error happens because a function has been used as a high order function (passed as an argument) and then the scope of this got lost. In such cases, I would recommend passing such function bound to this. E.g.

    this.myFunction.bind(this);
    

提交回复
热议问题