We can start from the outside then, from the following code:
videoPlayer.init(wrapper.querySelector(".video"));
When there are multiple setting being given to an init function, it’s preferable to give them as different object properties.
So to start, we move the wrapper information to an object property, which needs to be done to each call to the init function.
videoPlayer.init({
video: wrapper.querySelector(".video")
});
We can then refer to that video property from the init function.
// function init(video) {
function init(opts) {
loadPlayer();
timer = setInterval(function checkAPIReady() {
if (apiIsReady) {
timer = clearInterval(timer);
// addVideo(video);
addVideo(opts.video);
}
}, 100);
}
The reason why this is preferable is because it’s much better to use an object to contain multiple settings, than to give multiple parameters to a function. When reasons occur for more values to be given, adding another function parameter is much more disruptive than adding another object property instead.