function spinner(target, options = {}){ let defaults = {
message: "Please wait",
spinningSpeed: 5,
cssClass: ".is-spinning"
}; let settings = Object.assign(
{},
defaults,
options
); if(settings.spinningSpeed !== defaults.spinningSpeed){
settings.cssClass = _addSpeedClass(target);
} _renderSpinner(target, settings);
}
- Best pratices to write defaults as an objects, much clean and reuseable.
- Write an empty object {} to the object.assign is recommened, it avoid defaults been mutated.
- in if block, you see we might still use default value, so it is important to keep the origin defautls value.
Example2:
function spinner(target, options = {}) {
let defaults = {
message: "Please wait",
spinningSpeed: 5,
cssClass: ".is-spinning"
}; let settings = Object.assign({} , defaults , options); console.log(`Message: ${settings.message}`);
console.log(`spinningSpeed: ${settings.spinningSpeed}`);
console.log(`cssClass: ${settings.cssClass}`);
} spinner(targetElement, {
cssClass: ".is-fast-spinning",
spinningSpeed: 8
});