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
,
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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.