连锁打电话和一起申请的含义是什么? [英] What's the meaning to chain call and apply together?

查看:94
本文介绍了连锁打电话和一起申请的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsGarden中遇到了这个代码,我无法确定将 call apply 连在一起的含义。

 函数Foo(){} $ b $这两个函数都会使用给定的上下文对象执行函数,为什么它可以被链接? b 
Foo.prototype.method = function(a,b,c){
console.log(this,a,b,c);
};

//创建一个未绑定的method版本
//它需要参数:this,arg1,arg2 ... argN
Foo.method = function(){

//结果:Foo.prototype.method.call(this,arg1,arg2 ... argN)
Function.call.apply(Foo.prototype.method,arguments);
};


解决方案

它调用通过申请来呼叫;也就是说,它使用 call 来调用一个函数(method),它使用 apply 来拨打电话因为它以(几乎)数组的形式得到了参数。



所以把它分开:

  Function.call 

这是对<$ c

 函数实例上可用的$ c> call()函数。 Function.call.apply 

这是一个引用,通过引用调用函数,为 apply 。因为 apply 是通过调用对象引用的,所以当对的调用应用作为这个值将是对调用函数的引用。

  Function.call.apply(Foo.prototype.method,arguments); 

所以我们调用调用函数通过 apply ,并传递 Foo.prototype.method this value以及参数Foo.mmethod作为参数。

I think 它的效果基本相同:

  Foo.method = function(){
var obj = arguments [0],args = [] .slice .call(arguments,1);
Foo.prototype.method.apply(obj,args);
}

但我必须尝试确认。编辑是的,似乎是这样。所以我可以总结一下这个技巧的要点:当所需的 this 值是时,调用 apply()数组的第一个元素保存参数。换句话说,通常当你调用 apply()时,你已经有了这个对象引用,已经得到了参数(在一个数组中)。然而,在这里,因为这个想法是你将这个作为参数传递给它,所以需要将它分离出来,以便调用应用来制作。就我个人而言,我会像在我的翻译中那样做,因为它对我来说有点不那么灵活,但我想可以习惯它。根据我的经验,这不是一种常见的情况。


I come across this code in jsGarden, and I cannot figure the meaning to chain call and apply together. Both will execute the function with a given context object, why it could be chained?

function Foo() {}

Foo.prototype.method = function(a, b, c) {
    console.log(this, a, b, c);
};

// Create an unbound version of "method" 
// It takes the parameters: this, arg1, arg2...argN
Foo.method = function() {

    // Result: Foo.prototype.method.call(this, arg1, arg2... argN)
    Function.call.apply(Foo.prototype.method, arguments);
};

解决方案

It's making a call to call via apply; that is, it's using call to call a function ("method"), and it's using apply to make the call because it's got the arguments in the form of an (almost) array.

So to take it apart:

Function.call

That's a reference to the call() function available on all Function instances, inherited from the Function prototype.

Function.call.apply

That's a reference, via the reference to the call function, to apply. Because apply is referenced via the call object, when the call to apply is made the this value will be a reference to the call function.

Function.call.apply(Foo.prototype.method, arguments);

So we're invoking the call function via apply, and passing Foo.prototype.method to be the this value, and the arguments to "Foo.mmethod" as the arguments.

I think it's basically the same effect as this:

Foo.method = function() {
  var obj = arguments[0], args = [].slice.call(arguments, 1);
  Foo.prototype.method.apply(obj, args);
}

but I'll have to try it to make sure. edit Yes that seems to be it. So I can summarize the point of that trick as being a way to invoke apply() when the desired this value is the first element of the array holding the parameters. In other words, usually when you call apply() you've got the desired this object reference, and you've got the parameters (in an array). Here, however, since the idea is that you pass in the desired this as a parameter, then it needs to be separated out in order for a call to apply to be made. Personally I would do it as in my "translation" because it's a little less mind-bending (to me), but I suppose one could get used to it. Not a common situation, in my experience.

这篇关于连锁打电话和一起申请的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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