0

Hi I have a class named editor and I have a function called like this:

Editor.prototype.switchTab = function(e) {
   var self = this;
   console.log(self);
}

I then add an event listener to an element like this in another prototype function.

Editor.prototype.foo = function() {
    var self = this;
    var element = document.createElement('div');
    element.addEventListener('click', self.switchTab, false);
}

The problem is that in the switchTab function, this refers to the element, not the Editor class. How can I fix this?

Supradeep
  • 3,246
  • 1
  • 14
  • 28
Corentin Caer
  • 321
  • 3
  • 10

0 Answers0