Last active
September 12, 2018 11:16
-
-
Save manekinekko/1115223d6a18465a804441fd7c463a8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
constructor() { | |
this.counter = 0; | |
} | |
inc() { | |
this.counter += 1; | |
console.log(this); | |
}; | |
}; | |
var foo = new Foo(); | |
var element = document.querySelector('#foo'); | |
// cas #1 | |
element.addEventListener('click', foo.inc); //=> this === element | |
// cas #2 | |
element.addEventListener('click', () => foo.inc() ); //=> this === foo | |
// cas #3 | |
element.addEventListener('click', foo.inc.bind(foo)); //=> this === foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment