简体   繁体   English

为什么我不能调用function.apply?

[英]Why can I not call a function.apply?

This is related to this question: Is it possible to spread the input array into arguments? 这与以下问题有关: 是否可以将输入数组扩展为参数?

I'm assuming that given this line of code: 我假设给出以下代码:

Promise.all(array).then(foo)

Promise.all uses Function.call , to invoke foo , Promise.all使用Function.call来调用foo

foo.call(foo, arrayValues)

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. 我想将foo修改为foo.apply函数,以便使用值数组调用它可以将其拆分为常规参数。

Here is my train of thought.... 这是我的思路。

Assuming I have this function 假设我有这个功能

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

I can call this function using both call and apply 我可以同时使用callapply来调用此函数

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

However I cannot get this to work 但是我不能让它工作

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

What is happening here?? 这是怎么回事?

I got it to work 我上班了

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

equals to 等于

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

equals to 等于

test()

So you got undefined as output. 因此您未定义为输出。


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

equals to 等于

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

equals to 等于

test(1,2,3)

This is right. 这是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Safari会递归调用function.apply? - Why does Safari call function.apply recursively? 如果我使用function.apply(undefined,args),为什么将this设置为window? - Why is `this` set to `window` if I use `function.apply(undefined, args)`? 为什么调用 Function.apply.bind(fn, null) 调用的是 `fn.apply`,而不是 `Function.apply`? - Why call to Function.apply.bind(fn, null) calls `fn.apply`, and not `Function.apply`? Ajax调用和function.apply无限循环 - Ajax call and function.apply infinite loop 为什么javascript在传递给function.apply()或function.call()时会改变原始类型? - Why does javascript change primitive types when passed into function.apply() or function.call()? 为什么console.log()polyfill不使用Function.apply()? - Why do console.log() polyfills not use Function.apply()? 是否可以在不更改上下文的情况下调用function.apply? - Is it possible to call function.apply without changing the context? 为什么不通过function.apply()递归传递函数参数? - Why are function arguments not getting passed recursively via function.apply()? 最大的数组我可以安全地传递给function.apply()/ spread运算符 - Largest array I can safely pass to function.apply() / spread operator “回调不是函数”和function.apply() - “callback is not the function” and function.apply()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM