Created
May 5, 2016 22:19
-
-
Save santiago-puchginer-snkeos/d9c6107a40c10aa7e0d498b29e2ec19b to your computer and use it in GitHub Desktop.
Arrow functions in Javascript (ES6)
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
// Snippet for arrow functions (ES6) | |
// Example of a normal arrow function | |
let substract = (a, b) => { | |
return a - b; | |
} | |
// Example of a one-line arrow function | |
let add = (a, b) => a + b; | |
// Another useful example of arrow functions | |
let nums = [2, 3, 4, 5]; | |
let doubled = nums.map((n) => 2*n); | |
// Be aware that arrow functions have parent lexical scope, that is, "this" | |
// keyword refers to the parent of the current Object, not the Object itself | |
let person = { | |
name: "Ryan", | |
hobbies: ["Robots", "Reading"], | |
showHobbies() { | |
this.hobbies.forEach( (hobby) => console.log(`${this.name} likes ${hobby}`) ); | |
} | |
} | |
person.showHobbies(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment