This question has already been answered:

Question in the code:

class Target { constructor() { ... /*Следующий код передает функцию обработчик события через одну библиотеку, не важно что за библиотека, проблема в том что в this.targetOnMousedown я не могу получить ссылку на объект класса, то есть не могу что-то делать с объектом, this === undefined*/ this.target.on('mousedown', this.targetOnMousedown); } targetOnMousedown() { /*this не ссылается на объект Target, тут я например хочу сделать this.apples = 5;*/ } } 

Reported as a duplicate by participants. Duck Learns to Hide , VladD , Community Spirit 18 Sep '16 at 12:53 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    this depends on the context. To specify a context use .bind ()

     this.target.on('mousedown', this.targetOnMousedown.bind(this));