Making "super" work outside a literal?

Peter Michaux petermichaux at gmail.com
Sun Jun 19 11:14:18 PDT 2011


On Sun, Jun 19, 2011 at 10:56 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
> Exactly!
>
> One way to do this is to give methods an implicit parameter "here" (the object in which a method was found during dispatch), in addition to "this".

In some variant, "here" might make it possible to do what I was
describing as "sideways" calls?

http://www.mail-archive.com/es-discuss@mozilla.org/msg08069.html

> Then
>        super.foo(x, y)
> would desugar to
>        Object.getPrototypeOf(here).foo.call(this, x, y)
>
> With call() and apply(), you would have here === this.

When the prototype chain is three or more objects long, I'm wondering
what would happen.

With "here" I'd expect and think people would expect the following
behavior (but I don't think it is what your proposal of "here" would
do.)

function Alpha() {}
Alpha.prototype.one = function() {return 'Alpha.one';};
Alpha.prototype.two = function() {return 'Alpha.two';};
Alpha.prototype.three = function() {return super.one() + ' ' +
here.one() + ' ' + this.one();};

function Beta() {}
Beta.prototype = Object.create(Alpha.prototype);
Beta.prototype.one = function() {return 'Beta.one';};
Beta.prototype.two = function() {return super.one() + ' ' + here.one()
+ ' ' + this.one();};

function Gamma() {}
Gamma.prototype = Object.create(Beta.prototype);
Gamma.prototype.three = function() {return super.one() + ' ' +
here.one() + ' ' + this.one();};

var a = new Alpha();
var b = new Beta();
var g = new Gamma();

a.three(); // "Alpha.one Alpha.one Alpha.one"
b.two(); // "Alpha.one Beta.one Beta.one"
g.two(); // "Alpha.one Beta.one Gamma.one"
g.three(); // "Beta.one Gamma.one Gamma.one"

Peter


> On Jun 19, 2011, at 19:44 , Peter Michaux wrote:
>
>> On Sun, Jun 19, 2011 at 10:20 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
>>> It would be nice if "super" could work in any method and not just those methods that are defined inside an object literal. Then, a method would have to know what object it resides in, e.g. via an implicit parameter.
>>
>> So you want "super" to be dynamic but tied to the value of "this"
>> inside that function? Something like...
>>
>> function fn() {
>>    return this.alpha + super.beta();
>> }
>>
>> function Foo() {}
>> Foo.prototype.alpha = function() {return 1;};
>> Foo.prototype.beta = function() {return 2;};
>>
>> function Bar() {}
>> Bar.prototype = Object.create(Foo.prototype);
>> Bar.prototype.beta = function() {return 5;};
>>
>> var b = new Bar();
>> fn.call(b); // 1 + 2 = 3
>>
>> Peter
>>
>
> --
> Dr. Axel Rauschmayer
>
> axel at rauschma.de
> twitter.com/rauschma
>
> home: rauschma.de
> blog: 2ality.com
>
>
>
>


More information about the es-discuss mailing list