Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  164] [ 2]  / answers: 1 / hits: 43021  / 13 Years ago, thu, march 1, 2012, 12:00:00

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.


More From » javascript

 Answers
2

Meddling with array functions is not actually necessary.



Using rest parameter syntax ...rest is cleaner and more convenient.



Example





function argumentTest(first, ...rest) {
console.log(First arg: + first);

// loop through the rest of the parameters
for (let arg of rest) {
console.log(- + arg);
}
}

// call your function with any number of arguments
argumentTest(first arg, #2, more arguments, this is not an argument but a contradiction);





...Rest




[#87127] Wednesday, February 29, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
vaughns

Total Points: 20
Total Questions: 112
Total Answers: 112

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;