In JavaScript, functions are callable.
Can I remove this attribute from a function, leaving only a normal object?
var foo = function () {};
foo.[[callable]] = false; // pseudocode
foo(); // "foo is not a function"
No. Functions (the evaluation behaviour, not their object properties) are pretty much immutable.
The callability of objects is determined by whether they have a [[Call]] slot. The spec says about those:
Internal slots are allocated as part of the process of creating an object and may not be dynamically added to an object.
(and it is implied that they cannot dynamically be removed either). Furthermore, “internal methods are not part of the ECMAScript language” and “are not object properties”, which means they cannot be directly accessed, set, deleted or manipulated in any imaginable way by the means of the language.
[[Call]] slots are part of various objects, where they always contain an internal method, but they are never mutated except for their initialisation (once). They come in various kinds:
As you can see, there is no way to alter or even remove the [[Call]] slot of an object, not even on proxies. Your best bet is
make the function throw
when called in an inopportune state
create a new, uncallable object from the function by means of
var obj = Object.assign(Object.create(Object.getPrototypeOf(fn)), fn);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With