为什么要调用“apply”?而不是直接调用函数?
在查看 raphael 或 g.raphael 或其他库的源代码时,我注意到开发人员做了这样的事情:
var val = Math.max.apply(Math, data_array);
为什么不直接调用该函数,例如:
var val = Math.max(data_array);
谢谢。
When looking at the source code for raphael or g.raphael or other libraries I've noticed the developer does something like this:
var val = Math.max.apply(Math, data_array);
Why not just invoke the function directly, such as:
var val = Math.max(data_array);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Math.max 默认不接受列表。 “apply”允许您将列表解压缩为参数,以便 max 正常工作。
Math.max won't accept a list by default. "apply" allows you to unpack the list to arguments so that max works correctly.
我认为 Mozilla Docs 的解释很好地描述了它:
至于参数:
该文档给出了 apply 用例的一个很好的例子。在下面的示例中,apply 用于链接构造函数:
请注意,在
prod_dept
构造函数中,提供的this
引用了prod_dept
对象,arguments
是传递给product
构造函数的参数数组。I think the explanation from the Mozilla Docs describes it well:
As for the parameters:
The docs give a good example of a use case for apply. In the example below, apply is used to chain a constructor:
Notice that in the
prod_dept
constructor, thethis
supplied refers to theprod_dept
object, andarguments
is an array of arguments passed to theproduct
constructor.为什么调用“apply”而不是直接调用函数?让我们看一个片段:
在这里我们可以看到我们在函数中使用
'this'
(上下文),如果我们直接调用一个 func 而我们没有任何上下文,那么它将需要window
上下文,这是错误的。因此,如果您在函数中使用上下文,请使用apply
方法。Why invoke “apply” instead of calling function directly? Let's have a snippet:
Here we can see we are using
'this'
(context) within a function, If we directly call a func than we don't have any context, than it will takewindow
context, which is wrong. So, if you are using context within your function than useapply
method.当目的是使用参数值列表调用可变参数函数时,通常会使用
.apply
,例如Math.max([value1[,value2, ...]])
函数返回最大零个或多个数字。Math.max()
方法不允许您传入数组。如果您有一个需要获取最大值的值列表,通常可以使用 Function.prototype.apply(),例如但是,从 ECMAScript 6 开始,您可以使用 传播运算符:
使用可变参数运算符调用函数时,您甚至可以添加其他值,例如
.apply
is often used when the intention is to invoke a variadic function with a list of argument values, e.g.The
Math.max([value1[,value2, ...]])
function returns the largest of zero or more numbers.The
Math.max()
method doesn't allow you to pass in an array. If you have a list of values of which you need to get the largest, you would normally call this function using Function.prototype.apply(), e.g.However, as of the ECMAScript 6 you can use the spread operator:
When calling a function using the variadic operator, you can even add additional values, e.g.
通常,您可以使用
apply()
和call()
来设置调用函数所属的上下文或对象。然后,该函数有效地成为该对象/上下文的方法,如果您需要访问上下文的字段,这将很有帮助。这是 Javascript 原生调用函数的方式:
函数 参考: 理解 JavaScript 函数调用和“this” 作者:Yehuda Katz
因此,当直接调用函数时,您实际上是隐式地将默认上下文传递给函数。当您未使用
call()
或apply() 指定时,Javascript 会将
'window'
或'undefined'
作为上下文传递这个答案还给出了一个可能有助于理解用法的示例
Generally you would use
apply()
andcall()
to be able to set the context or the object to which the calling function belongs to. The function then effectively becomes a method of that object/context which is helpful if you'll need to access the fields of the context.This is how Javascript natively invokes functions:
Reference: Understanding JavaScript Function Invocation and "this" by Yehuda Katz
So, when calling a function directly you are actually implicitly passing in a default context to the function. Javascript passes
'window'
or'undefined'
as context when you don't specify it usingcall()
orapply()
This answer also gives an example which might help in understanding the usage