class sugar: static inheritance

Peter Michaux petermichaux at gmail.com
Sun Jun 5 21:35:37 PDT 2011


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. That is, static members are not inherited at all which does
not seem very class-like.

    function Monster() {
        Monster.allMonsters.push(this);
    }
    Monster.allMonsters = [];

    function Dragon() {}
    Dragon.prototype = Object.create(Monster.prototype);
    Dragon.prototype.constructor = Dragon;

Peter


More information about the es-discuss mailing list