Overriding default animation parameters with custom values Using JavaScript ES5, I would like to know if you could suggest me a more "elegant" and succinct way to overriding defaults variable when User pass custom values.

In the following example, an animation is executed using _defaultOptions, a developer can change the default behavior passing only some properties to a function, example:

animate(dom, {duration: 2000}); // overriding only duration, rest as default

Notes:


I need also to support IE 11.
optionsArg is optional, so animate(dom) is allowed




var dom = document.getElementById('logo');

var _defaultOptions = {
  duration: 1000,
  delay: 0,
  iterations: 1,
  direction: 'normal',
  fill: 'both'
};

var animate = function(dom, optionsArg) {
  // code to be improved here
  var options = {
    duration: optionsArg &amp;&amp; 'duration' in optionsArg ? optionsArg.duration : _defaultOptions.duration,
    delay: optionsArg &amp;&amp; 'delay' in optionsArg ? optionsArg.delay : _defaultOptions.delay,
    iterations: optionsArg &amp;&amp; 'iterations' in optionsArg ? optionsArg.iterations : _defaultOptions.iterations,
    direction: optionsArg &amp;&amp; 'direction' in optionsArg ? optionsArg.direction : _defaultOptions.direction,
    fill: optionsArg &amp;&amp; 'fill' in optionsArg ? optionsArg.fill : _defaultOptions.fill,
  };
  dom.animate([{
    opacity: 1
  }, {
    opacity: 0
  }], options);
};

animate(dom, {duration: 2000});
&lt;img id="logo" src="https://upload.wikimedia.org/wikipedia/commons/e/e5/NASA_logo.svg"&gt;
Object.assign(_defaultOptions, optionsArg )


But this will have side effects as it changes _defaultOptions object itself.