Adding combinePlayerOptions into existing code

Let’s go with the defaults.playerVars one at https://jsfiddle.net/xs9gL24c/

The combinePlayerOptions function can go between manageCover and videoPlayer, as that combinePlayerOptions function is intended to be used by the code below it.

In the videoPlayer’s addPlayer function we can update the following code:

    const defaultVars = defaults.playerVars;
    const playerVars = settings.playerVars;
    defaults.playerVars = Object.assign({}, defaultVars, playerVars);
    const player = new YT.Player(video, defaults);

and replace it using combinePlayerOptions

    const playerOptions = combinePlayerOptions(defaults, settings);
    const player = new YT.Player(video, playerOptions);

In the addPlayer’s createPlayerOptions function we can also replace the following code:

  function createPlayerOptions(settings) {
    const defaultOptions = defaults.playerVars;
    const defaultPlayerVars = defaultOptions.playerVars;
    const playerVars = Object.assign({}, defaultPlayerVars, settings);
    defaults.playerVars = playerVars;
    return defaults;
  }

It’s not appropriate for defaults to be updated there. Instead we just want to combine defaults and settings and return the combined lot.

  function createPlayerOptions(settings) {
    const playerOptions = combinePlayerOptions(defaults, settings);
    return playerOptions;
  }

When you use that combinePlayerOptions function you will no longer have any troubles happening when it comes to playerOptions and playerVars.

The updated code is found at https://jsfiddle.net/9zdbf6s0/

I now leave you to my once-per-drink frequency of replies as before.