Let’s assume I have a function:
var func = function(param){return param + 1;};
Given that functions are objects, I can add properties to it:
func.prop = 'foo';
Both func(4) and func.prop should work. But now let’s say that I have an object:
var obj = {prop: 'foo'};
…and I want to make it invocable as an increment function. Is this possible?
No.
Objects created by the Object constructor do not have in internal
[[Call]]method and therefore can’t be called. It’s the special[[Call]]method that makes a function a function. Note that the Object constructor is a Function, but it makes plain Objects, not functions.While Functions inherit from
Object.prototype, they are created by the Function constructor. Built–in constructors have extra powers given to them by ECMA-262. 🙂