Slicing arguments into an array.
The arguments keyword in javascript is not of type Array
, but can be turned into an Array
easily by using the slice function of the Array prototype.
function varargs() {
return Array.prototype.slice.apply(arguments);
}
> varargs(1, 2, 3);
[1, 2, 3]
Tweet