Here is an example of a pseudo class in an IIFE
// cow.js
(function(exports) {
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
Cow.prototype = {
greets: function(target) {
if (!target)
throw new Error("missing target");
return this.name + " greets " + target;
}
};
})(this);
What is the benefit of that over the following:
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
Cow.prototype = {
greets: function(target) {
if (!target)
throw new Error("missing target");
return this.name + " greets " + target;
}
};
Don't both of these end up adding the Cow 'constructor' function to the global scope?
The cow.js file is being included via the HTML document in a script tag. That means that the value of this
is window. Isn't that the same global scope that both examples will use to add the function to?
Could someone provide an example of using this in a module or different scope?
This is not a duplicate, as the IFFE in the following related question does not take a parameter - What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
The code was copied from here: https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/