将参数传递给另一个 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3914557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:37:11  来源:igfitidea点击:

Passing arguments forward to another javascript function

javascript

提问by Anders Nygaard

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?

在函数 a 中,我可以使用 arguments 关键字来访问参数数组,在函数 b 中这些都丢失了。有没有办法像我尝试做的那样将参数传递给另一个 javascript 函数?

回答by Nick Craver

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

用于.apply()argumentsin 函数具有相同的访问权限b,如下所示:

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

You can test it out here.

你可以在这里测试一下

回答by Walter Chapilliquen - wZVanG

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.

ECMAScript ES6 添加了一个新的运算符,可以让您以更实用的方式执行此操作... Spread Operator

Example without using the applymethod:

不使用该apply方法的示例:

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

a(1, 2, 3)

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

Note如果您的浏览器仍然使用 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 noin-page result.

编者按:由于代码片断使用console.log(),则必须打开浏览器的JS控制台看到的结果-将有没有在页面结果。

It will display this result:

它会显示这个结果:

Image of Spread operator arguments example

展开运算符参数示例的图像

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

简而言之,如果您使用数组,则扩展运算符可以用于不同的目的,因此它也可以用于函数参数,您可以在官方文档中看到类似的示例:Rest parameters

回答by Wayne

The explanation that none of the other answers supplies is that the original arguments arestill available, but not in the original position in the argumentsobject.

的解释,没有任何其他的答案用品的是,原来的参数仍然可用,但不是在原来的位置arguments的对象。

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

arguments对于提供给函数的每个实际参数,该对象包含一个元素。当您打电话时,您需要a提供三个参数:数字12、 和3。所以,arguments包含[1, 2, 3].

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

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

b但是,当您调用时,您只传递了一个参数:aarguments对象。所以arguments包含[[1, 2, 3]](即一个元素,它是aarguments对象,它的属性包含到 的原始参数a)。

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

a(1,2,3);

As @Nick demonstrated, you can use applyto provide a set argumentsobject in the call.

正如@Nick 所展示的,您可以使用在调用中apply提供一个集合arguments对象。

The following achieves the same result:

以下实现了相同的结果:

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

But applyis the correct solution in the general case.

apply在一般情况下是正确的解决方案。