Last active
November 26, 2017 17:52
-
-
Save RinatValiullov/715f18f8db75705ecc0cbd525a08530d to your computer and use it in GitHub Desktop.
Function list(), which return an array from arguments
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
// #1 With spread operator (ES6 syntax). Verbose, but interesting | |
let list1 = function(...args) { | |
let array = []; | |
for(let i = 0, len = args.length; i < len; ++i) { | |
array.push(args[i]); | |
}; | |
return array; | |
}; | |
// #1.1 Amazing arrow functions and spread syntax | |
let list1 = (...args) => { | |
let array = args; | |
return array; | |
}; | |
// #2 With array-like object -> arguments | |
let list = function() { | |
// Convert object arguments to array (make copy of initial array with slice method) | |
let args = Array.prototype.slice.call(arguments); | |
return args; | |
}; | |
/* | |
* list(8,6,4,2,11); // => [8, 6, 4, 2, 11] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment