Escape reserved keywords in object destructuring assignment

1.7k Views Asked by At

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 compile
class FooBar {
  constructor({foo, default}) {
    this.foo = foo;
    this.default = default;
  }
}
/* json from server {foo: "bar", default: true} */
new FooBar(json);
1

There are 1 best solutions below

1
On BEST ANSWER

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