How to remove the first argument of a function?

If I have a function, I can redirect all its arguments to a variable arguments.

Now I have a function that needs one (first) argument for itself and should only forward the rest.

For instance:

var calledFunction = function(num){
    //do something with num
    //remove num from the arguments
    forwardFunction(arguments);
}

I already tried arguments.shift();, but the arguments are just an โ€œarray-like objectโ€ and therefore do not know the function shift().

How to remove the first argument from the arguments and forward the remainder of the series of arguments?

+4
source share
3 answers

In ES6, the idiomatic way to write this would be

var calledFunction = function(num, ...args) {
  //do something with num
  forwardFunction(...args);
}
+4
source

, .slice():

    forwardFunction([].slice.call(arguments, 1));

"forwardFunction". , , , .apply():

    forwardFunction.apply(undefined, [].slice.call(arguments, 1));

, , , :

    var a = [];
    for (var i = 1; i < arguments.length; ++i)
      a[i] = arguments[i];
    forwardFunction(a); // or forwardFunction.apply(null, a);

( , .) arguments , . , , .

. sdgluck. , ; , , ES2015 .

+3

ES5:

forwardFunction(Array.prototype.slice.call(arguments, 1));

ES6:

forwardFunction([...arguments].slice(1));
+1
source

All Articles