4

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);
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
  • 1
    Seems like a better design to just not used reserved words since they're, y'know, reserved. – Paul Oct 03 '18 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 Feb 22 '22 at 18:30

1 Answers1

9

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;
  }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • **It's possible to use them as a property name, but not as a variable name**, solved my problem. I now use destructuring this way: `const { get, list, delete: deleteHandler } = handlerObj;` – Rvy Pandey Aug 01 '19 at 17:53