Arrow functions in ES6: The thing I didn’t know I wanted
Andy Zheng
52
Hey, nice article :) Like most of the times you need to use arrow function its when you want your scope to be preserved. Any others times just semantic sugar.
// Method functions
var Person = {
firstName: "First",
lastName: "Last",
nameArrow: () => this.firstName + " " + this.lastName,
nameNormal: function(){
return this.firstName + " " + this.lastName;
}
};
console.log(Person.nameArrow()); // prints 'undefined undefined'
console.log(Person.nameNormal()); // prints 'undefined undefined'
You have and mistake in here should be
console.log(Person.nameNormal()); // prints 'First Last'