Last active
January 5, 2017 09:46
-
-
Save Kishanjvaghela/32fb3152559e6b4e236c6715c6dd0491 to your computer and use it in GitHub Desktop.
Let's Learn ES6 - Arrow Functions
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
// 1 | |
let add = (a,b) => { | |
return a+b; | |
} | |
console.log(add(4,5)); | |
// 2 | |
let add = (a,b) => a+b; | |
console.log(add(4,5)); |
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
////////////////////////////// | |
//1 | |
let numbers = [1,2,3,4,5]; | |
let doubled = numbers.map(function(n){ | |
return n*2; | |
}); | |
console.log(doubled); | |
/* output | |
[2, 4, 6, 8, 10] | |
*/ | |
////////////////////////////// | |
// 2 | |
let numbers = [1,2,3,4,5]; | |
let doubled = numbers.map(n => n*2); | |
console.log(doubled); |
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
//1 | |
let person ={ | |
name: 'Kishan', | |
sayName: function() { | |
console.log(`I am ${this.name}`); | |
} | |
} | |
person.sayName(); | |
/* | |
output | |
"I am Kishan" | |
*/ | |
///////////////////////////////////////// | |
//2 | |
let person ={ | |
name: 'Kishan', | |
sayName: () => { | |
console.log(`I am ${this.name}`); | |
} | |
} | |
person.sayName(); | |
/* | |
TypeError: Cannot read property 'name' of undefined | |
at Object.sayName | |
Cz Arrow function have Lexical scope | |
Lexical Scope : scope of a variable so that it may only be called (referenced) | |
from within the block of code in which it is defined. | |
*/ | |
//////////////////////////////////// | |
//3 | |
let person ={ | |
name: 'Kishan', | |
sayName() { | |
console.log(`I am ${this.name}`); | |
} | |
} | |
person.sayName(); | |
/* | |
output | |
"I am Kishan" | |
*/ |
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
//1 | |
let person = { | |
name: 'Kishan', | |
hobbies: ['One','Two','Three'], | |
showHobbies: function(){ | |
this.hobbies.forEach(function(hobby){ | |
console.log(`${this.name} is ${hobby}`); | |
}); | |
} | |
} | |
person.showHobbies(); | |
/* | |
output: | |
"JS Bin Output is One" | |
"JS Bin Output is Two" | |
"JS Bin Output is Three" | |
*/ | |
///////////////////////////// | |
//2 | |
let person ={ | |
name: 'Kishan', | |
hobbies: ['One','Two','Three'], | |
showHobbies: function(){ | |
this.hobbies.forEach(hobby => { | |
console.log(`${this.name} is ${hobby}`); | |
}); | |
} | |
} | |
person.showHobbies(); | |
/* Output: | |
"Kishan is One" | |
"Kishan is Two" | |
"Kishan is Three" | |
*/ | |
////////////////// | |
//3 | |
let person ={ | |
name: 'Kishan', | |
hobbies: ['One','Two','Three'], | |
showHobbies: function(){ | |
let self = this; | |
this.hobbies.forEach(function(hobby){ | |
console.log(`${self.name} is ${hobby}`); | |
}); | |
} | |
} | |
person.showHobbies(); | |
/* Output: | |
"Kishan is One" | |
"Kishan is Two" | |
"Kishan is Three" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment