32

これはばかげているように聞こえるかもしれませんが、我慢してください。コンストラクターでオブジェクトをクラスプロパティに分解するための言語レベルでのサポートがあるかどうか疑問に思います。

class Human {
    // normally
    constructor({ firstname, lastname }) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.fullname = `${this.firstname} ${this.lastname}`;
    }

    // is this possible?
    // it doesn't have to be an assignment for `this`, just something
    // to assign a lot of properties in one statement
    constructor(human) {
        this = { firstname, lastname };
        this.fullname = `${this.firstname} ${this.lastname}`;
    }
}
4

1 に答える 1