【发布时间】:2023-03-31 19:05:01
【问题描述】:
注意:代码使用的是spread syntax,而不是rest parameters。
const fn1 = undefined
const args = []
const fn2 = () => {}
const fn3 = () => {}
console.log(fn1?.(...args, fn2, fn3))
错误:
console.log(fn1?.(...args, fn2, fn3))
^
TypeError: Function.prototype.apply was called on undefined, which is an undefined and not a function
【问题讨论】:
-
只有当
...args出现在函数调用末尾以外的任何其他位置时,才会出现问题。很奇怪。 -
我们收到这个错误很奇怪。通常,您会收到“fn1 不是函数”错误。即使不使用可选链接,也会出现此错误。比较:
(() => { ({test: undefined}).test(...([])) })() // Uncaught TypeError: {(intermediate value)}.test is not a function与(() => { ({test: undefined}).test(...([]), 1) })() // Uncaught TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function -
看起来像是 chromium 浏览器的问题,在 Mozilla Firefox 中运行良好
-
这绝对是一个错误——他们对可选链的实验性实现不适用于他们使用扩展语法转换调用的方式。
标签: javascript node.js google-chrome v8 optional-chaining