Skip to content
Advertisement

How can an object become callable like a function in Javascript?

So as long as anything in Javascript is actually an object, what makes an object behave as a function? What internal properties and labels make an object behave as an object we can call instead of just using it to store values?

Advertisement

Answer

There is an internal property, [[Call]], that determines what will be executed, when the object is called.

Plain objects don’t have this interal property, so they aren’t callable, and can’t be made to be callable.

The only callables in JS are functions (which are also objects), classes (which are actually functions, and therefore objects as well), and Proxy objects that wrap callables.

To create a callable object, create a function, and make it have the properties and prototypes you would like.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement