Last active
October 1, 2016 21:01
-
-
Save dnasca/ee1ea74dfc17c9bbf5ff69db85658584 to your computer and use it in GitHub Desktop.
a real use case of fat arrow functions
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
//when function scope sucks, and you have to cache 'this' | |
let a = function() { | |
let that = this; | |
this.val = 1; | |
setTimeout(function() { | |
that.val++; | |
console.log(that.val) | |
}, 1) | |
}; | |
//fat arrow to the rescue, 'this' in the context of setTimeout, now uses block scope | |
let a = function() { | |
this.val = 1; | |
setTimeout(() => { | |
this.val++; | |
console.log(this.val) | |
}, 1) | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment