I'm trying to write a class with optional named arguments.
class MyClass {
constructor({a=2, b=4, c=3}){
console.log(b);
}
}
If I create the object like this
let obj = new MyClass({});
then all is good.
However, if I create the object like this
let obj = new MyClass();
I get an error. Is it possible to make it accept empty parentheses?
CodePudding user response:
Have the whole argument also default to the empty object (in addition to the destructured properties defaulting to particular values).
class MyClass {
constructor({ a=2, b=4, c=3 } = {}){
console.log(b);
}
}
let obj = new MyClass();