将默认值分配给重命名的解构变量

时间:2019-04-29 16:20:18

标签: javascript object variables rename destructuring

如何为重命名的结构化变量分配默认值:

class MyClass {

    constructor ( { prop1, prop2, delete: remove, prop4 } ) {

        this.prop1  = prop1
        this.prop2  = prop2
        this.delete = delete
        this.prop4  = prop4

    }

}

1 个答案:

答案 0 :(得分:2)

正常销毁的默认值和销毁为另一个名称的默认值之间没有区别:

constructor ( { prop1 = true, prop2 = false, delete: remove = true, prop4 = 'something' } ) {

或者您可以选择:

  const defaults = { /*...*/ };

  constructor(settings) {
    Object.assign(this, defaults, settings);
  }

并避免所有麻烦:)