如何为重命名的结构化变量分配默认值:
class MyClass {
constructor ( { prop1, prop2, delete: remove, prop4 } ) {
this.prop1 = prop1
this.prop2 = prop2
this.delete = delete
this.prop4 = prop4
}
}
答案 0 :(得分:2)
正常销毁的默认值和销毁为另一个名称的默认值之间没有区别:
constructor ( { prop1 = true, prop2 = false, delete: remove = true, prop4 = 'something' } ) {
或者您可以选择:
const defaults = { /*...*/ };
constructor(settings) {
Object.assign(this, defaults, settings);
}
并避免所有麻烦:)