I’m trying to understand why we should use an immediately invoked function expression ( IIFE ) over just creating an object in this scenario.
Example using IIFE:
duck = { };
let motionModule = (function () {
return {
glideMixin: function (obj) {
obj.glide = function() {
console.log("Gliding on the water");
};
},
flyMixin: function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
};
}
}
}) ();
motionModule.glideMixin(duck);
duck.glide()
Versus just
duck2 = { };
let motionModule2 = {
glideMixin: function (obj) {
obj.glide = function() {
console.log("Gliding on the water");
};
},
flyMixin: function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
};
}
};
motionModule2.glideMixin(duck2);
duck2.glide()
Both of these execute console.log(“Gliding on the water”);