为什么要调用“应用”而不是直接调用函数? [英] Why invoke "apply" instead of calling function directly?

查看:225
本文介绍了为什么要调用“应用”而不是直接调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看raphael或g.raphael或其他库的源代码时,我注意到开发人员做了类似这样的事情:

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);

谢谢。

推荐答案

我认为 Mozilla Docs 描述得很好:


调用现有函数时,可以为其分配对象

引用当前对象
调用对象。使用apply,你可以
写一个方法一次,然后在另一个对象中继承
,而没有
来重写新
对象的方法。

You can assign a different this object when calling an existing function. this refers to the current object, the calling object. With apply, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

apply非常类似于call,除了
表示它支持的参数类型。
您可以使用参数数组,而不是
的一组命名参数。使用
apply,你可以使用一个数组文字,例如
,fun.apply(this,[name,
value])或一个Array对象,例如
, fun.apply(这个,新的
数组(名称,值))。

apply is very similar to call, except for the type of arguments it supports. You can use an arguments array instead of a named set of parameters. With apply, you can use an array literal, for example, fun.apply(this, [name, value]), or an Array object, for example, fun.apply(this, new Array(name, value)).

至于参数:


thisArg
确定内部的乐趣值。如果 thisArg 为null或
未定义,将是全局
对象。否则,这将等于
到Object(thisArg)(如果thisArg已经是对象,则为thisArg
,如果thisArg
为a,则为
String,Boolean或Number
对应类型的原始值)。因此,当函数执行时,
始终为true ==
object。

thisArg Determines the value of this inside fun. If thisArg is null or undefined, this will be the global object. Otherwise, this will be equal to Object(thisArg) (which is thisArg if thisArg is already an object, or a String, Boolean, or Number if thisArg is a primitive value of the corresponding type). Therefore, it is always true that typeof this == "object" when the function executes.

argsArray
对象的参数数组,指定应该调用
fun的参数,如果没有参数应该为函数提供
,则为null或
undefined。

argsArray An argument array for the object, specifying the arguments with which fun should be called, or null or undefined if no arguments should be provided to the function.

文档提供了一个应用用例的好例子。在下面的示例中,apply用于链接构造函数:

The docs give a good example of a use case for apply. In the example below, apply is used to chain a constructor:

function product(name, value)
{
  this.name = name;
  if (value >= 1000)
    this.value = 999;
  else
    this.value = value;
}

function prod_dept(name, value, dept)
{
  this.dept = dept;
  product.apply(this, arguments);
}
prod_dept.prototype = new product();

// since 5 is less than 1000 value is set
var cheese = new prod_dept("feta", 5, "food");

// since 5000 is above 1000, value will be 999
var car = new prod_dept("honda", 5000, "auto");

请注意,在 prod_dept 构造函数中, 提供此是指 prod_dept 对象,参数是传递给 product 构造函数的参数数组。

Notice that in the prod_dept constructor, the this supplied refers to the prod_dept object, and arguments is an array of arguments passed to the product constructor.

这篇关于为什么要调用“应用”而不是直接调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆