gmosx.ninja

Access JavaScript function arguments as an Array

One rather cool feature of JavaScript is that you can access a method's arguments from a special variable conveniently named ...arguments. Quite annoyingly though, the arguments variable is not a real array. Sure, you can get the arguments length or iterate through it's members. But you cannot call other Array methods like .push(), .shift() etc..

Fear not, the following one-liner converts the arguments variable to a real Array:

var args = Array.prototype.splice.call(arguments, 0);

The Array .splice() method is injected into the special arguments object to extract it's members and return a new Array.