Escape reserved keywords in object destructing assignment

左心房为你撑大大i 提交于 2019-12-13 03:24:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!