Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Chaining - Function.prototype.apply was called on undefined, which is an undefined and not a function

Using optional chaining with function calls causes the expression to automatically return undefined instead of throwing an exception if the method isn't found.

Note: The code is using spread syntax, not rest parameters.

const fn1 = undefined
const args = []
const fn2 = () => {}
const fn3 = () => {}

console.log(fn1?.(...args, fn2, fn3))

Error:

console.log(fn1?.(...args, fn2, fn3))
                                ^
TypeError: Function.prototype.apply was called on undefined, which is an undefined and not a function
like image 860
Wenfang Du Avatar asked Mar 13 '21 02:03

Wenfang Du


People also ask

How do I enable optional chaining?

To enable optional chaining, you need to install a package. At the time of writing, optional chaining is not natively supported in Javascript, it is a new feature introduced in ES2020. Until it is fully adopted we can get all the optional goodness by installing a package!

What does Optional chaining do?

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil , the property, method, or subscript call returns nil .

What is the difference between using call () and apply () to invoke a function with multiple arguments?

The Difference Between call() and apply() The difference is: The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method is very handy if you want to use an array instead of an argument list.

What does apply () do in Javascript?

apply() The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).


1 Answers

It turns out to be a V8 bug, I've submitted it there, hopefully, it'll be fixed soon.

Update: it has been fixed.

like image 52
Wenfang Du Avatar answered Sep 28 '22 17:09

Wenfang Du