在 JavaScript 中使用参数调用 apply 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7676768/
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
Call apply method with arguments in JavaScript
提问by dkugappi
I wanted to invoke a function using the javascript apply() method. This works fine if the function has no arguments. i.e.
我想使用 javascript apply() 方法调用一个函数。如果函数没有参数,这可以正常工作。IE
function test()
{
console.log(this);
}
body = document.getElementsByTagName("body")[0]; // shortcut to body element
test.apply(body); // returns [object HTMLBodyElement]
But I can't seem to do the same thing to invoke a function that has arguments:
但我似乎不能做同样的事情来调用一个有参数的函数:
function test(msg)
{
console.log(msg);
}
body = document.getElementsByTagName("body")[0]; // shortcut to body element
test(this).apply(body); // mysteriously returns the correct result, then
// typeError: 'undefined' is not an object (evaluating "test(this).apply".
The examples above are completely trivial but the grain of my question is: How do I use the apply() method to invoke a function with arguments.
上面的例子完全是微不足道的,但我的问题的核心是:如何使用 apply() 方法来调用带参数的函数。
回答by John Keyes
apply
takes two arguments:
apply
需要两个参数:
test.apply(null, [body]);
The first argument sets the value of this
when the function is executed. The second is an array of parameters for that function.
第一个参数设置this
函数执行时的值。第二个是该函数的参数数组。
In your examples you could rewrite it as follows:
在您的示例中,您可以将其重写如下:
function test() {
console.log(this);
}
and call it as follows:
并按如下方式调用它:
test.apply(this);
回答by CMS
First notice that you are actually invoking your function immediately, the line:
首先请注意,您实际上是在立即调用您的函数,该行:
test(this).apply(body);
Fails because at first, you call the test
function passing this
as an argument, and then you try to access an apply
property on the function's result, which is undefined
since your function doesn't return anything.
失败,因为首先,您调用作为参数test
传递this
的函数,然后您尝试访问apply
函数结果的属性,这是undefined
因为您的函数不返回任何内容。
That property access on the undefined
value is giving you the TypeError
exception.
对该undefined
值的属性访问为您提供了TypeError
例外。
Now, let's see what you actually want to do, if you want to pass the value of the body
variable as the argument of your function, setting the outer this
value, as the this
value of test
, you can do:
现在,让我们看看您真正想要做什么,如果您想将body
变量的值作为函数的参数传递,将外部this
值设置为 的this
值test
,您可以执行以下操作:
test.apply(this, [body]);
But that's why the call
method exists, when you know exactly which arguments to pass, you don't need to create an array for nothing, you just pass the arguments:
但这就是该call
方法存在的原因,当您确切地知道要传递哪些参数时,您无需无缘无故地创建数组,只需传递参数即可:
test.call(this, body);
The apply
method is really useful when you deal with for example a dynamic number of arguments, when you know which arguments want to pass, and still have the ability of setting the this
value, call
is just what you need.
apply
当您处理例如动态数量的参数时,该方法非常有用,当您知道要传递哪些参数并且仍然具有设置this
值的能力时,call
这正是您所需要的。
回答by bittersweetryan
With apply you send an array of arguments as its second argument:
使用 apply 你发送一个参数数组作为它的第二个参数:
test.apply(body,["Hello World"]);
Here is the apply documentation on the MDN docs https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply
这是 MDN 文档上的应用文档https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply
回答by bbg
Be sure to add in the argument:
请务必在参数中添加:
test.apply(body, ["my message here"]);