为什么我不能.call()Function.call? [英] Why can't I .call() Function.call?
问题描述
在Javascript中, Function.call()
可以在给出 this <的情况下调用
Function
/ code>值和零个或多个参数。
In Javascript, Function.call()
can call Function
given a this
value and zero or more arguments.
Function.call
本身是一个函数。因此,从理论上讲, Function.call
应该与 Function.call.call
相同(或类似地起作用)。
Function.call
itself is a function. So in theory, Function.call
should be the same (or similarly acting) function as Function.call.call
.
在V8中,情况似乎是这样的:
In V8, this seems to be the case:
> Function.call === Function.call.call
true
当我们调用 Function.call()
,我们得到一个匿名函数
When we call Function.call()
, we get an anonymous function
> Function.call()
[Function: anonymous]
但是,我不能打电话 .call()
在 Function.call
上。
However, I can't call .call()
on Function.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
,如错误消息所提示的那样。
What is going on here? Function.call
is clearly a function - it's not undefined
as this error message suggests.
推荐答案
简短答案:错误消息是非常令人误解。
Short answer: The error message is very misleading. It is the same error message you get when you do
(undefined)();
更长的答案:
第二个 .call()
用 this $ c调用
Function.call
中的$ c>。
The second .call()
is being invoked with a this
of Function.call
.
不带参数调用会导致它调用 this
且未定义
作为此
值。
Calling it with no parameters causes it to call this
with undefined
as the this
value.
因此,您确实在做
Function.call.call(undefined)
这意味着(隐喻地)您正在做
which means you're (metaphorically) doing
undefined.call()
这实际上只是
undefined()
不传递任何内容(或 undefined
) Function.call.call()
的 this
参数本质上是否定 this
第一个 Function.call()
的上下文(这本身就是 Function
本身),导致在未定义
上调用 .call()
。
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
.
这将产生错误消息:未定义不是函数
。
这篇关于为什么我不能.call()Function.call?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!