I have a simple click handler
$(\'#test\').click( ev => {
var $test = $(this);
console.log($test
Yury Tarabanko has already answered your question: Arrow functions do not bind this
. This can come in handy if you're writing something like this:
// JS:
class Foo{
constructor( $button ){
this.$button = $button;
$button.click(() => {
this.sayHello();
});
}
sayHello() {
alert('Hi');
}
}
new Foo( $('button') );
Check out this fiddle: https://jsfiddle.net/bhkkLfty/
Play with this code for a while. Just change () => {}
into function() {}
and console.log this
.
Edit: For more information, check out one of the following articles: