在 JavaScript 中,Function.call()
可以打电话Function
给定一个this
值和零个或多个参数。
Function.call
本身就是一个函数。所以理论上来说,Function.call
应该是相同(或类似作用)的功能Function.call.call
.
在V8中,情况似乎是这样:
> Function.call === Function.call.call
true
当我们打电话时Function.call()
,我们得到一个匿名函数
> Function.call()
[Function: anonymous]
但是,我不能打电话.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
正如此错误消息所示。
简短回答:错误信息是very误导。这与您执行此操作时收到的错误消息相同
(undefined)();
更长的答案:
第二.call()
正在被调用this
of Function.call
.
不带参数调用它会导致它调用this
with undefined
as the this
value.
因此,你真的在做
Function.call.call(undefined)
这意味着你正在(比喻地)做
undefined.call()
这真的只是
undefined()
不传递任何内容(或undefined
)到this
的参数Function.call.call()
本质上就是否定this
第一个的上下文Function.call()
(这只是Function
本身),导致.call()
被调用于undefined
.
这会产生以下错误消息:undefined is not a function
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)