Skip to content

Instantly share code, notes, and snippets.

@vjefri
Last active May 14, 2016 09:14
Show Gist options
  • Save vjefri/512e4eb607450344d3cfa6c1bd5c841e to your computer and use it in GitHub Desktop.
Save vjefri/512e4eb607450344d3cfa6c1bd5c841e to your computer and use it in GitHub Desktop.
// This does not loose scope with fat arrow
var bob = {
_name: "Bob",
_friends: ["Jon", "Daenerys", "Arya", "Tyrion"],
printFriends() {
this._friends.forEach(f => // this === bob
console.log(this._name + " knows " + f)); // this === bob ("I should be undefined but I am bob")
}
}
/*
Output:
Bob knows Jon
Bob knows Daenerys
Bob knows Arya
Bob knows Tyrion
*/
var bob = {
_name: "Bob",
_friends: ["Jon", "Daenerys", "Arya", "Tyrion"],
printFriends() {
this._friends.forEach(function(f){ // this === bob
console.log(this._name + " knows " + f); // this === bob ("I knew you wouldn't believe me")
})
}
}
/*
Output:
undefined knows Jon
undefined knows Daenerys
undefined knows Arya
undefined knows Tyrion
*/
bob.printFriends()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment