Why can't I .call() Function.call?

前端 未结 1 1969
囚心锁ツ
囚心锁ツ 2021-01-14 16:27

In Javascript, Function.call() can call Function given a this value and zero or more arguments.

Function.call its

1条回答
  •  有刺的猬
    2021-01-14 16:53

    Short answer: The error message is very misleading. It is the same error message you get when you do

    (undefined)();
    

    Longer answer:

    The second .call() is being invoked with a this of Function.call.

    Calling it with no parameters causes it to call this with undefined as the this value.

    Therefore, you're really doing

    Function.call.call(undefined)
    

    which means you're (metaphorically) doing

    undefined.call()
    

    which is really just

    undefined()
    

    Passing nothing (or undefined) to the this parameter of Function.call.call() is essentially negating the this context of the first Function.call() (which would be just Function itself), causing .call() to be invoked on undefined.

    This yields the error message that is produced: undefined is not a function.

    0 讨论(0)
提交回复
热议问题