The ECMAScript specification function IsCallable returns true iff its argument has a [[Call]] internal method. It is used in several places in the specification, such as in the definition of Array.prototype.toString
.
There is a similar specification function IsConstructor which returns true iff its argument has a [[Construct]] internal method.
Some JS objects, including most built-in functions such as escape
are callable but not constructible. Are there any which are constructible but not callable?
Note that both user-defined and built-in classes throw TypeError
when called as ordinary functions, but are still callable per the definition of IsCallable, as can be determined by seeing if Array.prototype.toString
will attempt to use them as the implementation of join
:
// {} is not callable, so toString falls back to Object.prototype.toString:
console.log('null:', Array.prototype.toString.apply({join: {}}));
// WeakMap is callable (but throws TypeError):
console.log('null:', Array.prototype.toString.apply({join: WeakMap}));
// User-defined classes also callable:
console.log('null:', Array.prototype.toString.apply({join: class Foo {}}));