Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active October 1, 2016 21:01
Show Gist options
  • Save dnasca/ee1ea74dfc17c9bbf5ff69db85658584 to your computer and use it in GitHub Desktop.
Save dnasca/ee1ea74dfc17c9bbf5ff69db85658584 to your computer and use it in GitHub Desktop.
a real use case of fat arrow functions
//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