Last active
January 5, 2017 13:10
-
-
Save Kishanjvaghela/782937c48fc3e8c79dbb6def0d1b8309 to your computer and use it in GitHub Desktop.
Let's Learn ES6 - Spread Operator and Rest Parameters
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 sum = function(){ | |
var method = (prev,curr)=>{ | |
return prev+curr; | |
}; | |
return Array.prototype.reduce.call(arguments,method); | |
}; | |
console.log(sum(1,2,3,4)); | |
/* | |
output: | |
10 | |
*/ | |
////////////////////// | |
//2-A | |
let sum = function(...args){ | |
var method = (prev,curr)=>{ | |
return prev+curr; | |
}; | |
return args.reduce(method); | |
}; | |
console.log(sum(1,2,3,4)); | |
////////////////////// | |
//2-B | |
let sum = function(...args){ | |
var method = (prev,curr) => prev+curr; | |
return args.reduce(method); | |
}; | |
console.log(sum(1,2,3,4)); | |
////////////////////// | |
//2-C | |
let sum = function(...args){ | |
return args.reduce((prev,curr) => prev+curr); | |
}; | |
console.log(sum(1,2,3,4)); | |
/* | |
output: | |
10 | |
*/ | |
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 sum = function(... args){ | |
console.log(args); | |
}; | |
sum(1,2,3,4); | |
/* output: | |
[1, 2, 3, 4] | |
*/ |
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
let multiply = (mul,...numbers)=>{ | |
return numbers.map(n=>{ | |
return mul*n; | |
}); | |
}; | |
let result = multiply(10,1,2,3); | |
console.log(result); | |
/**output: | |
[10, 20, 30] | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment