I\'ve created a class and I would like to set some default options for values in case the user does not supply any arguments. I recently went from a constructor that took mu
You could either set a default value for options, i.e {}
.
class User {
constructor(options = {}) {
this.name = options.name || "Joe";
this.age = options.age || 47;
}
}
or first check for options
to be truthy and then access the value.
class User {
constructor(options) {
this.name = options && options.name || "Joe";
this.age = options && options.age || 47;
}
}