Created
December 29, 2015 14:13
-
-
Save eshacker/9e70517b3a74b721344c to your computer and use it in GitHub Desktop.
Arrow functions and lexical this
This file contains 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
"use strict"; | |
var Person = function Person(name, age, sex, friends) { | |
var self = this !== undefined ? this : Object.create(Person.prototype); | |
self.Name = name; | |
self.Age = age; | |
self.Sex = sex; | |
self.Friends = friends; | |
self.SayHelloToMyFriends = function () { | |
var _this = this; | |
this.Friends.forEach(function (f) { | |
return console.log(_this.Name, "says hello to", f); | |
}); | |
}; | |
return self; | |
}; | |
var threeIdiots = [new Person("Mark", "25", "F", ["zorg", "xorg", "xergc"]), new Person("Arthur", "25", "F", ["lancelot", "boastalot", "talkalot"]), new Person("Trump", "35", "F", ["ed", "edd", "eddy"])]; | |
threeIdiots.forEach(function (person) { | |
person.Friends.forEach(function (x) { | |
return console.log(person.Name, "has a friend named", x); | |
}); | |
person.SayHelloToMyFriends(); | |
}); |
This file contains 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
var Person = function(name, age, sex, friends) { | |
var self = this !== undefined ? this : Object.create(Person.prototype); | |
self.Name = name; | |
self.Age = age; | |
self.Sex = sex; | |
self.Friends = friends; | |
self.SayHelloToMyFriends = function(){ | |
this.Friends.forEach(f => console.log(this.Name, "says hello to", f)); | |
}; | |
return self; | |
}; | |
var threeIdiots = [ | |
new Person("Mark", "25", "F", ["zorg", "xorg", "xergc"]), | |
new Person("Arthur", "25", "F", ["lancelot", "boastalot", "talkalot"]), | |
new Person("Trump", "35", "F", ["ed", "edd", "eddy"]) | |
]; | |
threeIdiots.forEach(person => { | |
person.Friends.forEach(x => console.log(person.Name, "has a friend named", x)); | |
person.SayHelloToMyFriends(); | |
}); |
This file contains 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
Mark has a friend named zorg | |
Mark has a friend named xorg | |
Mark has a friend named xergc | |
Mark says hello to zorg | |
Mark says hello to xorg | |
Mark says hello to xergc | |
Arthur has a friend named lancelot | |
Arthur has a friend named boastalot | |
Arthur has a friend named talkalot | |
Arthur says hello to lancelot | |
Arthur says hello to boastalot | |
Arthur says hello to talkalot | |
Trump has a friend named ed | |
Trump has a friend named edd | |
Trump has a friend named eddy | |
Trump says hello to ed | |
Trump says hello to edd | |
Trump says hello to eddy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment