3

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?

Community
  • 1
  • 1
Zhami
  • 19,033
  • 14
  • 48
  • 47
  • 2
    To help clarify, can you add an example describing why you want to do this? e.g. I need this because I'm ____________________. Before getting into the nitty gritty, I think we'd want to make sure we are solving your problem, and not just creating "Astronaut Architecture" – scunliffe Jun 01 '11 at 19:22
  • Because I want to have prototypal inheritance for functions, and not have to resort to adding methods to Function.prototype, because the added methods won't work on arbitrary native types and objects. – Zhami Jun 01 '11 at 19:26
  • That said, more specifically, I have a Constructor that produces function objects. I want to have methods that work only for those function objects. Presently, I can refer to instance.constructor.prototype to get to the methods that act on these function objects, e.g.: instance.constructor.prototype.method(); But it sure would be nice to modify the [[prototype]] chain so that I could simply write instance.method(); – Zhami Jun 01 '11 at 19:31
  • I think adding some (pseudo-)code to your question would clarify what you mean. – KooiInc Jun 01 '11 at 21:03
  • Anybody reading my question: If you are not sure of what I'm asking, you may want to be sure you have a solid understanding of [[prototype]], \_\_proto\_\_, and the way that Function instances don't inherently behave like Object instances even though they are Object instances. Some good background on this page: http://javascript.info/tutorial/inheritance – Zhami Jun 02 '11 at 00:24

3 Answers3

1

In V8 (and most other browsers/engines except IE) you can change an object's prototype by setting the __prototype__ __proto__ attribute. Setting the prototype attribute 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 no Function.create or Array.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:

  1. To be able to call aFunction you must initialize it with an actual function, eg:

    var aFunction = function () {};
    

    This is important. Calling a function does not access the prototype chain, so if you define aFunction as an object and simply set the __proto__ attribute, you will not able to call aFunction.

  2. 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 on aFunction itself.

  3. Calling aFunction() will always invoke the function that was originally declared when aFunction was defined and will never invoke aFunction'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.

Daniel Baulig
  • 10,739
  • 6
  • 44
  • 43
  • Interesting how many write-ups state that functions are first class objects. Well, they may inherit from Object, but they aren't really first class objects in the sense that they don't have a [[prototype]] chain. I haven't read read Brendan's blog post, but I suspect your spot on about his understanding the need for inheritance with Functions and Arrays. Thanks for the link... I'm off to read now. – Zhami Jun 01 '11 at 20:43
  • This looks promising Daniel -- I will give it a try. I know you meant \_\_proto\_\_ -- but good of you to correct for others who may visit here. – Zhami Jun 02 '11 at 00:04
  • Daniel -- setting \_\_proto\_\_ works like a charm. Alas, I have cold feet, because doing this is non-standard and I suddenly feel like the library I am developing could be useful in the Browser, and so would like to find a cross-browser solution, but suspect there isn't one. Anyway, thanks for your diligent research and reporting! – Zhami Jun 02 '11 at 00:51
  • There is no cross browser solution. IE does not support modification of the special `[[prototype]]` attribute at all. – Daniel Baulig Jun 02 '11 at 00:55
  • Will just have to wait for Harmony :-) Another way to handle this is by proxies, which are proposed for Harmony (see: http://wiki.ecmascript.org/doku.php?id=harmony:proxies). Isaac Schlueter has implemented this (non-standard!) as a CC-coded extension for V8 under node.JS (see: https://github.com/isaacs/node-proxy) – Zhami Jun 02 '11 at 01:08
  • Posting the above comment inspired me to write a very simple proxy (actually, a method forwarder and invoker). I have explained this in an answer I posted below. – Zhami Jun 02 '11 at 01:52
1

I came up with a solution that solves my needs. It is not cross-browser, but can be used cross-browser. My most important use case is as a module for node.JS. In that case, the mechanism of setting __proto__ works just fine, in which case I can call methods on the base function object

f.method(args...);

and it executed by code in the "super" function. Because the method is invoked by the method invocation pattern, "this" is set to the base function object, and so the proper properties are accessed even though the method resides in the "super."

Now for the in-Browser case: when I use my library client-side, I provide a proxy mechanism. Alas, code intended for the browser must be written differently. The invocation is:

f.proxy(methodName, args...);

The proxy method in the base function object is:

f.proxy = function (methodName) {
    var p = this.constructor.prototype;
    return p.proxy(this, methodName, arguments);
};

The proxy in the "super" object's prototype is:

proxy: function (instance, methodName) {
    var args = Array.prototype.splice.apply(arguments, [2]), 
        method = this[methodName];
    return (method) ? method.apply(instance, args) : undefined;
}

I probably should have named this "forward" or some such, but "proxy" is good enough.

Perhaps this mechanism might be useful to someone...

Zhami
  • 19,033
  • 14
  • 48
  • 47
  • Note: that code is sloppy -- the super.proxy should either (1) examine the type of the supplied methodName (because it is really just a property name), and apply if a function, otherwise return the value; or (2) enforce use of methods and have getter methods for any properties that should be exposed. – Zhami Jun 02 '11 at 02:04
0

I think I understand what you're trying to do. In short, there's no way to really do it natively. You'd have to access the Function constructor, which for function expressions and definitions (i.e. anything using the 'function' keyword), isn't possible as far as I can tell. You could overwrite Function and just always use new Function([args], string) but I doubt you (or any JS programmer) want to do that.

Your best bet would probably be to send your function expressions to another function that returns the function object with your custom methods dynamically added:

wrapFunc = function(f){
  f.customFunc = someCustomFunc;
  return f;
}

var myNewFunc = wrapFunc(
  function(){
    //do something
  }
);

myNewFunc.customFunc();
Matt Molnar
  • 2,412
  • 3
  • 22
  • 28
  • Thanks for your thoughts, but that won't do what I want as you are still using the function operator to create the function. – Zhami Jun 01 '11 at 20:39