Javascript ES6类构造函数中的解构

Javascript ES6类构造函数中的解构,javascript,ecmascript-6,destructuring,Javascript,Ecmascript 6,Destructuring,这听起来可能很可笑,但请容忍我。我想知道在语言层面上是否支持在构造函数中将对象分解为类属性,例如 class Human { // normally constructor({ firstname, lastname }) { this.firstname = firstname; this.lastname = lastname; this.fullname = `${this.firstname} ${this.lastname

这听起来可能很可笑,但请容忍我。我想知道在语言层面上是否支持在构造函数中将对象分解为类属性,例如

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

您不能将
分配给该语言中的任何位置

一个选项是合并到此
或其他对象:

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

干杯我知道我们不能分配给这个,但希望有类似的东西。我非常喜欢使用分解结构,因为它有一个清晰的模式,即告诉我正在分配哪些属性。但无论如何,谢谢你的回答。或者你需要这个?构造函数({firstname,lastname}){Object.assign(this,{firstname,lastename});this.fullname=
${this.firstname}${this.lastname}
;}@r03你尝试过这个解决方案吗?它确实有效?为什么不起作用?这个答案中没有解构。如果你想
fullname
保留
firstname
lastname
中的更改,请使用getter@Jan right谢谢。对不起,这是个坏例子。我只想证明,在
firstname
lastname
之后,如果有意义的话,会有更多的初始化。
这个
不能被分配到-在ES5中永远不会,在ES6中唯一改变其“值”的是
super()
。但要在其上指定属性,请参见副本。