为什么我不能.call() Function.call?

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

在 Javascript 中,Function.call() 可以在给定 this 值和零个或多个参数的情况下调用 Function

Function.call本身就是一个函数。因此理论上,Function.call 应该与 Function.call.call.

具有相同(或类似作用)的功能

在V8中,好像是这样的:

> Function.call === Function.call.call
true

当我们调用Function.call()时,我们得到一个匿名函数

> Function.call()
[Function: anonymous]

但是,我无法在 Function.call 上调用 .call()

> Function.call.call()
TypeError: undefined is not a function
at repl:1:21
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:291:14)
at REPLServer.runBound [as eval] (domain.js:304:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
at ReadStream.onkeypress (readline.js:109:10)

这是怎么回事? Function.call 显然是一个函数 - 它不是此错误消息所暗示的 undefined

简短回答:错误消息非常具有误导性。这与您在

时收到的错误消息相同
(undefined)();

更长的答案:

正在使用 Function.callthis 调用第二个 .call()

不带参数调用它会导致它以 undefined 作为 this 值调用 this

所以,你真的在​​做

Function.call.call(undefined)

这意味着你(比喻地)在做

undefined.call()

这真的只是

undefined()

不向 Function.call.call()this 参数传递任何内容(或 undefined)实质上是否定第一个 Function.call()this 上下文(这将只是 Function 本身),导致 .call()undefined.

上被调用

这会产生错误消息:undefined is not a function