class sugar: static inheritance

Mark S. Miller erights at google.com
Mon Jun 6 07:38:23 PDT 2011


On Sun, Jun 5, 2011 at 9:35 PM, Peter Michaux <petermichaux at gmail.com>wrote:

> Are static members inherited? What happens in the last line of the
> following code?
>
>    class Monster {
>        static allMonsters = [];
>
>        constructor() {
>            Monster.allMonsters.push(this);
>        }
>    }
>
>    class Dragon extends Monster {
>        constructor() {}
>    }
>
>    new Monster();
>    Monster.allMonsters.length; // 1
>    Dragon.allMonsters.length;  // 0, 1, or error?
>
> Based on my understanding of what the desugared code would be, the
> last line above would be an error because Dragon.allMonsters is
> undefined.


Correct. And the desugaring you show below is correct. This question is
listed as an open issue at the bottom of <
http://wiki.ecmascript.org/doku.php?id=harmony:classes#open_issues>, so it's
possible we'd change our minds. I can see both sides on this one. But on the
whole, I think we are better off sticking with the current design. A
tremendous amount of old code manually encodes classes into prototypes using
the common pattern that the current proposal desugars into.

Once the authors of that old code are no longer worried about pre-ES6
platforms, were classes to adopt static inheritance, then:

When old code using the common pattern is manually translated in the obvious
way to classes, its meaning will change in non-obvious ways.


Of all the pros and cons for this issue, this seems like a compelling con.



> That is, static members are not inherited at all which does
> not seem very class-like.
>

Depends on what classes tradition you come from. There are languages on both
sides of this dichotomy.



>
>    function Monster() {
>        Monster.allMonsters.push(this);
>    }
>    Monster.allMonsters = [];
>
>    function Dragon() {}
>    Dragon.prototype = Object.create(Monster.prototype);
>    Dragon.prototype.constructor = Dragon;
>
> Peter
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110606/cb1d41e8/attachment.html>


More information about the es-discuss mailing list