在对象破坏分配中转义保留关键字



是否可以在对象破坏赋值中使用保留关键字。

具体来说,我正在尝试使用名为默认的属性处理 JSON。

//Doesn't compile
class FooBar {
constructor({foo, default}) {
this.foo = foo;
this.default = default;
}
}


/* json from server {foo: "bar", default: true} */
new FooBar(json);

可以将它们用作属性名称,但不能用作变量名称。选择其他目标:

class FooBar {
constructor({foo, default: def}) {
this.foo = foo;
this.default = def;
}
}

最新更新