admin管理员组

文章数量:1406937

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

本文标签: javascriptEscape reserved keywords in object destructuring assignmentStack Overflow