将调用和应用链接在一起是什么意思? [英] What's the meaning to chain call and apply together?

查看:19
本文介绍了将调用和应用链接在一起是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 jsGarden 中遇到了这段代码,我无法理解将 callapply 链接在一起的含义.两者都将使用给定的上下文对象执行函数,为什么可以链接?

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

推荐答案

它正在通过 apply 调用 call;也就是说,它使用 call 来调用一个函数(method"),它使用 apply 来进行调用,因为它有一个(几乎) 数组.

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.

所以把它拆开:

Function.call

这是对所有 Function 实例上可用的 call() 函数的引用,继承自 Function 原型.

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

Function.call.apply

这是一个引用,通过对call 函数的引用,对apply 进行引用.因为 apply 是通过 call 对象引用的,所以当调用 apply 时,this 值将是一个引用 call 函数.

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

所以我们通过apply调用call函数,并将Foo.prototype.method传递给this 值,以及Foo.mmethod"的参数作为参数.

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

但我必须尝试以确保.edit 是的,似乎就是这样.所以我可以总结这个技巧的要点是当所需的 this 值是保存参数的数组的第一个元素时调用 apply() 的方法.换句话说,通常当您调用 apply() 时,您已获得所需的 this 对象引用,并且您已获得参数(在数组中).但是,这里的想法是将所需的 this 作为参数传入,因此需要将其分离出来以便调用 apply.就我个人而言,我会像在我的翻译"中那样做,因为它不那么令人费解(对我而言),但我想人们可以习惯它.根据我的经验,这并不常见.

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天全站免登陆