I have this code, which is kinda long winded but gets there in the end …
class WorksButLongWinded {
constructor (callback, params = {}) {
// defaults ...
this.foo = "string";
this.bar = 200;
this.callback = callback;
// params ...
if( typeof params.foo != "undefined" ) { this.foo = params.foo }
if( typeof params.bar != "undefined" ) { this.bar = params.bar }
}
}
I thought I might try using destructuring to speed things along, like this:
class DoesNotWork {
constructor (callback, params = {}) {
{
this.foo = "string",
this.bar = 200
} = params;
this.callback = callback;
}
}
... except that doesn't work. It doesn't even pass syntax muster.
What is a clean and simple way of writing a class constructor that takes an optional params object with various optional parameters that override some default values?