What is the best I can get Code 1 & Code 2 to be short of using a code library?
Both codes currently work as they are, but both may need to be written a little differently, is what you may be saying.
Work as they are, meaning, the settings and playlists all work in the codes.
Whichever code you would like to start with first, unless, the instructions to adjust both would be simple to do.
Code 1 uses this:
https://jsfiddle.net/c0a7kdws/
const defaults = {
playerOptions: {
playerVars: {
autoplay: 0,
controls: 0,
disablekb: 1,
enablejsapi: 1,
fs: 0,
iv_load_policy: 3,
rel: 0
}
}
};
function createPlayerOptions(settings) {
const defaultOptions = defaults.playerOptions;
const playerOptions = defaultOptions.playerVars;
const playerVars = Object.assign({}, playerOptions, settings);
playerOptions.playerVars = playerVars;
return playerOptions;
}
function init(playerOptions) {
Object.assign(defaults.playerOptions, playerOptions);
}
Code 2 uses this:
https://jsfiddle.net/xs9gL24c/
const defaults = {};
defaults.playerVars = {
autoplay: 0,
controls: 0,
disablekb: 1,
enablejsapi: 1,
fs: 0,
iv_load_policy: 3
};
function createPlayerOptions(settings) {
const defaultOptions = defaults.playerVars;
const defaultPlayerVars = defaultOptions.playerVars;
const playerVars = Object.assign({}, defaultPlayerVars, settings);
defaults.playerVars = playerVars;
return defaults;
}
function init(playerOptions) {
Object.assign(defaults, playerOptions);
}