问题
Is it possible to use reserved keywords in an object destructing assignment.
Specifically I am trying to handle JSON with property property named default.
//Doesn't compile
class FooBar {
constructor({foo, default}) {
this.foo = foo;
this.default = default;
}
}
/* json from server {foo: "bar", default: true} */
new FooBar(json);
回答1:
It's possible to use them as a property name, but not as a variable name. Choose a different target:
class FooBar {
constructor({foo, default: def}) {
this.foo = foo;
this.default = def;
}
}
来源:https://stackoverflow.com/questions/52635676/escape-reserved-keywords-in-object-destructing-assignment