I'm trying to define a constructor with the following behavior for an options argument:
class Test {
constructor({
someOption = 'foo',
otherOption = 'bar',
aSeedOfSomeSort = Math.random(),
// ...
}) {
console.log(
someOption, otherOption, aSeedOfSomeSort
);
}
}
new Test({someOption: 'lol'});
new Test({otherOption: 'derp'});
new Test({aSeedOfSomeSort: 0.5});
new Test({});
new Test(); // here's the problem
This works great, however, I want the constructor to work so that to use all default parameters, I don't have to pass an empty object. I don't care if the object itself is named or not in the arguments, but in the context of the constructor, I want a clean way to directly access all the options without a namespace or using with
.
Is this possible?