I am doing the title-described task, and I am wondering what is the point of this pattern. I have read some of the topics here on the subject but none addressed my doubts totally. I will be glad for some further explanation.
As you can see bellow I have 2 patterns, both of them work, but FCC and people seem to recommend the first one, because it’s not polluting the “global namespace”. I wonder, how does it pollute global namespace when in both cases both functions are available only by refferencing the variable “funModule”? So how is this polluting and what exactly?
Are there any more adventages for the firsrt pattern in comparison to the second, apart the forementioned and apart the fact that we can add some code to execute before the “return” statement(where is this useful btw)?
When to use the first and when the second of patterns?
Regards
// let funModule = (function() {
// return {
// isCuteMixin: function(obj) {
// obj.isCute = function() {
// return true;
// };
// },
// singMixin: function(obj) {
// obj.sing = function() {
// console.log("Singing to an awesome tune");
// };
// }
// }
// })();
let funModule = {
isCuteMixin: function(obj) {
obj.isCute = function() {
return true;
};
},
singMixin: function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
}
};
var duck = {};
funModule.singMixin(duck);
duck.sing();
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
.
Challenge: Use an IIFE to Create a Module
Link to the challenge: