I would like to create function objects (yes, all functions are objects) with some control over the prototypal inheritance, that is, I would like one function to inherit from another.
I can make objects that have prototypal inheritance, and know to set the prototype property of a function performing as a constructor to initialize the [[prototype]] property of the object.
However, when creating a function, I must use the function operator or the Function constructor. I could try to swizzle Function.prototype, but (1) don’t know if that is writable, and (2) that just seems quite dangerous [still, I should try doing that].
btw: I only care to do this for V8 within node.JS, so if there are means that work only for that environment, that would be acceptable.
For the record, I have seen this:
Is it possible to create a function with another prototype than Function.prototype?
In V8 (and most other browsers/engines except IE) you can change an object’s prototype by setting the
__prototype____proto__attribute. Setting theprototypeattribute will instead change the prototype that is used to create an object if the function is invoked as a constructor function. This should not be what you want.Afaik there currently is no standard conform way to directly “subclass” a function (or array for that matter). There’s only
Object.create, but there is noFunction.createorArray.create.EDIT:
I just realized that function objects do not have the__prototype__attribute and changing / setting it will not turn an object into a function.I believe though that I just recently watched a talk by Brendan Eich (the creator of JavaScript) in which he talked about Function and Array equivalents of
Object.create. And infact, googling for “Function.create brendan eich” reveals the following blog post by Eich in which he talks about his wish to implement Function.create and Array.create.EDIT 2: Ok, I screwed up. Pretty much. The non-standard attribute is of course
__proto__and not__prototype__. Setting__proto__works fine for functions with some restrictions, which are:To be able to call
aFunctionyou must initialize it with an actual function, eg:This is important. Calling a function does not access the prototype chain, so if you define
aFunctionas an object and simply set the__proto__attribute, you will not able to callaFunction.You can now assign any other function to
aFunction.__proto__and reading any members (including methods) will correctly delegate to the prototype chain if the porperty is not found onaFunctionitself.Calling
aFunction()will always invoke the function that was originally declared whenaFunctionwas defined and will never invokeaFunction‘s prototype function. So the body of the function is not subject to inheritence.Sorry for screwing up first with the name of the attribute. Hope this helps you nevertheless.