你如何在 ES Next 中扩展静态属性?

How do you extend static properties in ES Next?

有时(尽管很少)您需要扩展而不是覆盖父静态属性,例如下面的(非常蹩脚的)示例:

class Person {
    static coreStats = {
        role: "slacker",
        weapon: null,
        speed: 4,
        vechicle: "roller blades",
    };
}

class Ninja extends Person {
    static coreStats = {
        role: "silent assassin",
        weapon: "katana",
        speed: 99,
    };
}

console.log(Ninja.coreStats); // But vechicle is no more

如何在不破坏父属性的情况下扩展子 class 中的静态属性?

你可以这样做:

class Person {
    static coreStats = {
        role: "slacker",
        weapon: null,
        speed: 4,
        vechicle: "roller blades",
    };
}

class Ninja extends Person {
    static coreStats = Object.assign({}, Person.coreStats, {
        role: "silent assassin",
        weapon: "katana",
        speed: 99
    });
}
console.log(Ninja.coreStats);

这将合并 coreStats 并在重复的情况下用 Ninja.coreStats 覆盖 Person.coreStats