Dark Launch

JavaScript function with default options

Use Object.assign to merge objects.

Javascript
const original = {
    'a': 1,
    'b': 2,
    'c': 0,
};
 
const updates = {
    'c': 3,
    'd': 4,
};
 
const result = Object.assign(original, updates);
console.log('result:', result);
Code
result: { a: 1, b: 2, c: 3, d: 4 }

Create a function that has default options by using Object.assign.

Javascript
function doThing(options) {
    var defaults = {
        'debug': false,
        'retryCount': 0,
    };
    options = Object.assign(defaults, options);
    console.log(options);
}
Javascript
doThing();
// { debug: false, retryCount: 0 }
 
doThing({'debug': false, 'retryCount': 1});
// { debug: false, retryCount: 1 }
 
doThing({'debug': true, 'retryCount': 5});
// { debug: true, retryCount: 5 }