javascript - Escape reserved keywords in object destructuring assignment - Stack Overflow

Is it possible to use reserved keywords in an object destructuring assignment?Specifically I am trying

Is it possible to use reserved keywords in an object destructuring assignment?

Specifically I am trying to handle JSON with property named default.

//Doesn't pile
class FooBar {
  constructor({foo, default}) {
    this.foo = foo;
    this.default = default;
  }
}
/* json from server {foo: "bar", default: true} */
new FooBar(json);

Is it possible to use reserved keywords in an object destructuring assignment?

Specifically I am trying to handle JSON with property named default.

//Doesn't pile
class FooBar {
  constructor({foo, default}) {
    this.foo = foo;
    this.default = default;
  }
}
/* json from server {foo: "bar", default: true} */
new FooBar(json);
Share Improve this question edited Oct 1, 2023 at 12:59 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Oct 3, 2018 at 21:03 Steven WexlerSteven Wexler 17.4k8 gold badges57 silver badges83 bronze badges 2
  • 1 Seems like a better design to just not used reserved words since they're, y'know, reserved. – Paul Commented Oct 3, 2018 at 21:07
  • @Paul when working with vue + storybook, using the default keyword for slots is normal, and removing from the args is also quite useful. – Gustavo Fenilli Commented Feb 22, 2022 at 18:30
Add a ment  | 

1 Answer 1

Reset to default 10

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;
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744374411a4571113.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信