Tell us what’s happening:
What’s the difference between using IIFE (immediately invoked function expression) and just declaring functions in a module object. For example:
Using IIFE
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!");
};
}
}
}) ();
Directly creating a module object
let motionModule = {
glideMixin: function(obj) {
obj.glide = function() {
console.log("Gliding on the water");
};
},
flyMixin: function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
};
}
};
The second way doesn’t seem to pollute the global variables and it even pass the tests, so why is the lesson guiding us to create a module using IIFE (or is it just to present IIFE?)?
Link to the challenge: