Created
August 25, 2018 17:17
-
-
Save mojaray2k/f9e108264c121bc294f39f289998d890 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/diqumox
This file contains hidden or 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
/** Rest and Spread Operators */ | |
// Example #1 Rest Operator | |
function adddNumbers(...numbers) { | |
return numbers.reduce((sum, number) => { | |
return sum + number; | |
}, 0) | |
} | |
const totalNumbers = adddNumbers(1,2,3,4,5,6,7); | |
console.log(totalNumbers); | |
// Example #2 Spread Operator | |
const defaultColors = ['red', 'green']; | |
const userFavoriteColors = ['orange', 'yellow']; | |
const fallColors = ['fire red', 'fall orange'] | |
const mergeColors = ['blue', 'pink', ...fallColors, ...defaultColors, ...userFavoriteColors]; | |
console.log(mergeColors); | |
// Ezample #3 Rest and Spread Operator | |
function validateShoppingList(...items) { | |
if(items.indexOf('milk') < 0) { | |
return ['milk', ...items] | |
} | |
return items; | |
} | |
const shoppingList = validateShoppingList('oranges', 'bread', 'eggs') | |
console.log(shoppingList); | |
// Example #4 Rest Operator | |
const MathLibrary = { | |
calculateProduct(...rest){ | |
console.log('Please us the multiply method instead') | |
return this.multiply(...rest); | |
}, | |
multiply(a,b){ | |
return a * b; | |
} | |
}; | |
const mathLibraryResult = MathLibrary.calculateProduct(8, 15); | |
console.log(mathLibraryResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment