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.