Last active
June 7, 2016 05:08
-
-
Save stefanfrede/f55aa49dee6098e4a79c to your computer and use it in GitHub Desktop.
Convert arguments, an array-like object, into an array.
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
/** | |
* Common practice to convert arguments into an array: | |
*/ | |
var args = Array.prototype.slice.call(arguments); | |
/* or the shorthand */ | |
var args = [].slice.call(arguments); | |
/** | |
* If you want an array of the arguments that lets you | |
* use the optimization of the V8 JavaScript engine used | |
* in Chrome and Node you need to resort to this: | |
*/ | |
var args = new Array(arguments.length); | |
for(var i = 0; i < args.length; ++i) { | |
args[i] = arguments[i]; | |
} | |
/** | |
* Or if you are using ES2015 you can use the | |
* spread operator ...: | |
*/ | |
const args = [...arguments]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment