admin管理员组

文章数量:1129172

I've tried the following with no success:

function a(args){
    b(arguments);
}

function b(args){
    // arguments are lost?
}

a(1,2,3);

In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. Is there a way of passing arguments to another javascript function like I try to do?

I've tried the following with no success:

function a(args){
    b(arguments);
}

function b(args){
    // arguments are lost?
}

a(1,2,3);

In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. Is there a way of passing arguments to another javascript function like I try to do?

Share Improve this question asked Oct 12, 2010 at 12:18 Anders NygaardAnders Nygaard 5,5532 gold badges23 silver badges31 bronze badges 2
  • 3 @BobStein-VisiBone Agreed. Plus, note that arguments is not actually an array (but rather an object that implements array-like semantics) and therefore it is not entirely clear at first glance whether it can be used in the same way as an actual array can. – Jules Commented Nov 27, 2017 at 1:58
  • @Jules still vote to reopen after all these years. What's the badge for burninating other people's valuable work? A lot of those to go around. – Bob Stein Commented Apr 24, 2020 at 18:56
Add a comment  | 

6 Answers 6

Reset to default 589

Use .apply() to have the same access to arguments in function b, like this:

function a(){
    b.apply(null, arguments);
}
function b(){
    console.log(arguments); //arguments[0] = 1, etc
}
a(1,2,3);

You can test it out here.

Spread operator

The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

ECMAScript ES6 added a new operator that lets you do this in a more practical way: ...Spread Operator.

Example without using the apply method:

function a(...args){
  b(...args);
  b(6, ...args, 8) // You can even add more elements
}
function b(){
  console.log(arguments)
}

a(1, 2, 3)

Note This snippet returns a syntax error if your browser still uses ES5.

Editor's note: Since the snippet uses console.log(), you must open your browser's JS console to see the result - there will be no in-page result.

It will display this result:

In short, the spread operator can be used for different purposes if you're using arrays, so it can also be used for function arguments, you can see a similar example explained in the official docs: Rest parameters

The explanation that none of the other answers supplies is that the original arguments are still available, but not in the original position in the arguments object.

The arguments object contains one element for each actual parameter provided to the function. When you call a you supply three arguments: the numbers 1, 2, and, 3. So, arguments contains [1, 2, 3].

function a(args){
    console.log(arguments) // [1, 2, 3]
    b(arguments);
}

When you call b, however, you pass exactly one argument: a's arguments object. So arguments contains [[1, 2, 3]] (i.e. one element, which is a's arguments object, which has properties containing the original arguments to a).

function b(args){
    // arguments are lost?
    console.log(arguments) // [[1, 2, 3]]
}

a(1,2,3);

As @Nick demonstrated, you can use apply to provide a set arguments object in the call.

The following achieves the same result:

function a(args){
    b(arguments[0], arguments[1], arguments[2]); // three arguments
}

But apply is the correct solution in the general case.

If you want to only pass certain arguments, you can do so like this:

Foo.bar(TheClass, 'theMethod', 'arg1', 'arg2')

Foo.js

bar (obj, method, ...args) {
  obj[method](...args)
}

obj and method are used by the bar() method, while the rest of args are passed to the actual call.

If you want to use this in Typescript, you may get an error.

A spread argument must either have a tuple type or be passed to a rest parameter.

To fix this you need to use Parameters<Type> to constructs a tuple type from the types used in the parameters of a function.

function a(...args: Parameters<typeof b>) { 
  b(...args); 
}

I've created this answer from some comments on this page, which could be harder to find for anyone trying to find this.

This one works like a charm.

function a(){
    b(...arguments);
}

function b(){
    for(var i=0;i<arguments.length;i++){
       //you can use arguments[i] here.
    }
}

a(1,2,3);

本文标签: Passing arguments forward to another javascript functionStack Overflow