Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a slice from "arguments"

Tags:

javascript

All you know that arguments is a special object that holds all the arguments passed to the function.

And as long as it is not an array - you cannot use something like arguments.slice(1).

So the question - how to slice everything but first element from arguments?

UPD:

seems like there is no way without converting it to an array with

var args = Array.prototype.slice.call(arguments); 

If someone posts another solution it would be great, if not - I'll check the first one with the line above as an answer.

like image 598
zerkms Avatar asked Mar 01 '12 03:03

zerkms


People also ask

What does args Slice do?

javascript - How does args. slice(1) separate the arguments that are passed into PhantomJS and forEach(function(arg, i)...

How can you get the total number of arguments passed to a function?

The arguments. length property contains the number of arguments passed to the function.

How can you get the type of arguments passed to a function?

There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.


1 Answers

Q. How to slice everything but first element from arguments?

The following will return an array containing all arguments except the first:

var slicedArgs = Array.prototype.slice.call(arguments, 1); 

You don't have to convert arguments to an array first, do it all in one step.

like image 87
nnnnnn Avatar answered Nov 01 '22 09:11

nnnnnn