(function(window){
var helloSpeaker = new Object ();
var speakWord = "Hello";
helloSpeaker.speak = function (name) {
console.log(speakWord + " " + name);
}
window.helloSpeaker = helloSpeaker;
})(window);
From the snippet above, I understand that it's an immediately invoked function expression(IIFE) which its major purpose is to protect the private variable 'speakWord'. I'm also awared that we need to expose the object 'helloSpeaker' to the window scope which is the reason for window.helloSpeaker = helloSpeaker;
. However, i seem not to understand why we need to pass window as an argument to the function knowing fully well that it's still in the window environment where the function is invoked.
Please why do I need to pass window to the function?