You do need to pass something to the function, so one option would be to try something like this:
class greatFunction {
defaultOptions = {
names: { first: 'Joe', last: 'Bloggs' }
}
constructor(options) {
if (options == null)
options = this.defaultOptions;
console.log(options.names.first + ' ' + options.names.last);
}
}
You can then create a new greatFunction using:
let x = new greatFunction(null)
Or:
let y = new greatFunction({ names: { first: 'Test', last: 'Blah' } });
Alternatively, don't do it using the constructor, and use a property after initialising. For example:
class greatFunction {
public options = {
names: { first: 'Joe', last: 'Bloggs' }
}
constructor() { }
doSomething() {
console.log(this.options.names.first + ' ' + this.options.names.last);
}
}
and then use with:
let x = new greatFunction();
x.options = { names: { first: 'Test', last: 'Blah' } };
x.doSomething();
options.names.last = options.name.last || "default"if the first side of the check (left from||) is true, that value is taken, if it is false, the right side is taken. Since undefined is false, it defaults to"default"