Skip to content

Instantly share code, notes, and snippets.

@RinatValiullov
Last active November 26, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RinatValiullov/715f18f8db75705ecc0cbd525a08530d to your computer and use it in GitHub Desktop.
Save RinatValiullov/715f18f8db75705ecc0cbd525a08530d to your computer and use it in GitHub Desktop.
Function list(), which return an array from arguments
// #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