Подтвердить что ты не робот

ES6 Уничтожение в конструкторе класса

Это может показаться смешным, но нести меня. Интересно, есть ли поддержка на уровне языка для деструкции объекта в свойства класса в конструкторе, например.

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}`;
    }
}
4b9b3361

Ответ 1

Вы не можете назначить this в любом месте языка.

Один из вариантов заключается в объединении в this или другом объекте:

constructor(human) {
  Object.assign(this, human);
}