Last active
May 14, 2016 09:14
-
-
Save vjefri/512e4eb607450344d3cfa6c1bd5c841e 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
// 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