为什么我不能调用function.apply? [英] Why can I not call a function.apply?

查看:68
本文介绍了为什么我不能调用function.apply?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此问题有关:
是否可以将输入数组扩展到参数中?

我假设给定这行代码:

Promise.all(array).then(foo)

Promise.all 使用 Function.call 来调用 foo

foo.call(foo, arrayValues)

我想将 foo 修改为 foo。应用函数,以便使用值数组调用它将其拆分为常规参数。

I would like to modify foo into a foo.apply function such that calling it with an array of values splits it into regular arguments.

这是我的思路......

Here is my train of thought....

假设我有这个功能

function test(a,b,c){
    console.log(a,b,c)
}

我可以使用调用 apply

test.call(null,1,2,3)
>> 1 2 3
test.apply(null,[1,2,3])
>> 1 2 3

到目前为止一切顺利,这也有效......

So far so good, this also works...

test.call.apply(test,[null,1,2,3])
>> 1 2 3

但是我无法让它工作

test.apply.call(test,[null,1,2,3])
>> undefined undefined undefined

这里发生了什么?

推荐答案

test.apply.call(test,[null,1,2,3])

等于

test.apply([null,1,2,3])

等于

test()

所以你没有定义作为输出。

So you got undefined as output.

test.apply.call(test,null,[1,2,3])

等于

test.apply(null,[1,2,3])

等于

test(1,2,3)

这是对的。

这篇关于为什么我不能调用function.apply?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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