> Can you show an example (and also the same example which is solved by 
> es-proposed super-calls)? 
> 
> The technique I showed of course initially is designed to be used with 
> class-system; though, I think it can be adopted to class-free system as well.



Code taken from https://gist.github.com/1330574#L68

Object.defineProperty(Object.prototype, "super", {
  value: function (method) {
    let proto = Object.getPrototypeOf(this);

    // we should use exactly this link and not
    // proto.__proto__ i.e. Object.getPrototypeOf(proto)
    // since in other case we can go away to i-looping
    let parentProto = proto.__parentProto__;

    // remove "__parentProto__" property from
    // our prototype to avoid i-looping; at 3rd and
    // later levels the property will be found just deeper
    delete proto.__parentProto__;

    // call the parent method
    parentProto[method].apply(this, [].slice.call(arguments, 1));

    // and restore "__parentProto__" back
    Object.defineProperty(proto, "__parentProto__", {
      value: parentProto,
      writable: true,
      configurable: true
    });
  }
});

Two more comments:

- The code does not work if an instance method makes a super-call.

- No matter where you are in a prototype chain, you will always delete and 
restore __parentProto__ in the second chain member (in the prototype of 
|this|). Have you tested it with more than two recursive super-calls?

What might work is something like the following:

When you look for super.foo(), traverse the prototype chain, starting at 
|this|. Look for the *second* occurrence of "foo". Record the object where you 
have found it in this["obj_of_super_"+"foo"] (which you delete after the 
super-call returns). In subsequent super-calls, you can start your search at 
this.obj_of_super_foo (instead of |this|).

The above effectively records where a method has been found, but does it more 
indirectly (and waits until super has been called).

However, this still fails when one of the super.foo() starts to call this.foo() 
(which is perfectly OK if there are parameters involved that prevent an 
infinite recursion).

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to