在javascript中调用并应用 [英] call and apply in javascript

查看:62
本文介绍了在javascript中调用并应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

链接呼叫和一起申请的含义是什么?

我找到了这样的代码:

function fun() {
    return Function.prototype.call.apply(Array.prototype.slice, arguments);
}

我知道电话在js中应用,但是当他们走到一起时我很困惑。

I know the call and apply in js,however I am confused when they come together.

然后我想知道是否

Function.prototype.call.apply(Array.prototype.slice, arguments)

与以下内容相同:

Array.prototype.slice.apply(arguments);

如果没有,第一行会做什么?

If not,what does the first line do?

推荐答案

好吧,让我们通过替换来解决这个问题。我们从:

Alright, let's tackle this problem via substitution. We start with:

Function.prototype.call.apply(Array.prototype.slice, arguments);

我们所知道的:


  1. Function.prototype.call 是一个函数。

  2. this 指针调用指向 Function.prototype

  3. 我们使用 apply 指针的指针更改为 code> Array.prototype.slice 。

  4. 参数 已应用(未作为参数传递)到调用

  1. Function.prototype.call is a function.
  2. The this pointer of call points to Function.prototype.
  3. We use apply to change the this pointer of call to Array.prototype.slice.
  4. arguments is applied (not passed as a parameter) to call.

因此上面的语句相当于:

Thus the above statement is equivalent to:

Array.prototype.slice.call(arguments[0], arguments[1], ...);

从此我们看到:


  1. Array.prototype.slice 是一个函数。

  2. this 指针切片指向 Array.prototype

  3. 我们使用调用指针 slice 更改为 arguments [0]

  4. arguments [1],... 作为参数传递给切片

  1. Array.prototype.slice is a function.
  2. The this pointer of slice points to Array.prototype.
  3. We use call to change the this pointer of slice to arguments[0].
  4. arguments[1], ... are passed as parameters to slice.

这与以下内容相同:

arguments[0].slice(arguments[1], ...);

这样做的好处是我们正在创建一个 slice的Stack Overflow>快速未绑定包装器在一行中。

The advantage of this is that we're creating a fast unbound wrapper for slice in a single line.

编辑:创建快速未绑定包装器的更好方法如下(请注意,它可能不起作用一些旧的浏览器,但你现在不需要担心 - 你可能总是使用 shim 适用于不支持 bind )的浏览器:

A better way to create fast unbound wrappers is as follows (note that it may not work in some older browsers, but you don't really need to worry about that now - you may always use a shim for browsers which don't support bind):

var slice = Function.prototype.call.bind(Array.prototype.slice);

这与以下内容相同:

function slice() {
    return Function.prototype.call.apply(Array.prototype.slice, arguments);
}

工作原理:


  1. Function.prototype.call 是一个函数。

  2. 指针调用指向 Function.prototype

  3. 我们使用 bind 更改指针的调用 Array.prototype.slice

  4. bind 返回一个参数已应用的函数

  1. Function.prototype.call is a function.
  2. The this pointer of call points to Function.prototype.
  3. We use bind to change the this pointer of call to Array.prototype.slice.
  4. bind returns a function whose arguments are applied to call.

奖励:如果您的编程风格功能强大,就像我的一样,那么您会发现这段代码非常有用:

Bonus: If your style of programming is highly functional, like mine is, then you would find that is piece of code is very useful:

var funct = Function.prototype;
var obj = Object.prototype;
var arr = Array.prototype;

var bind = funct.bind;

var unbind = bind.bind(bind);
var call = unbind(funct.call);
var apply = unbind(funct.apply);

var classOf = call(obj.toString);
var ownPropertyOf = call(obj.hasOwnProperty);
var concatenate = call(arr.concat);
var arrayFrom = call(arr.slice);




  1. 使用此功能,您可以使用<$ c $轻松创建未绑定的包装器c>致电或申请

  2. 您可以使用 classOf 获取值的内部 [[Class]]

  3. 您可以使用 ownPropertyOf in for for loops。

  4. 您可以使用 concatenate 来连接数组。

  5. 您可以使用 arrayFrom 来创建数组。

  1. Using this you may easily create unbound wrappers using either call or apply.
  2. You may use classOf to get the internal [[Class]] of a value.
  3. You may use ownPropertyOf inside for in loops.
  4. You may use concatenate to join arrays.
  5. You may use arrayFrom to create arrays.

这篇关于在javascript中调用并应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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