Reputation: 1263
I've found a code where it was structured like this:
(function() {
function Usermanager(user) {
...
...
// other properties
}
//prototype methods
// And then noticed this line:
window.Usermanager = Usermanager;
}()
Isn't this line alternative to module exports in the old syntax where modules are not available?
window.Usermanager = Usermanager;
If yes then what is the point of adding it in IIFE function which provides the feature of keeping everything private.
Upvotes: 0
Views: 52
Reputation: 544
it is not equivalent to es6 modules. in the old days you know we haven't private variables and also we need an encapsulation to make our code more effective so we benefit from the function scope and borrow IEFE to do that so let me show you how was it. here is an old way to make encapsulation using IEFE:
(function(win, doc){
var _private = "private";
win.public = "public";
})(window, document);
I hope this little explaining is helpful.
Upvotes: 0
Reputation: 943569
Isn't this line alternative to module exports
Not really. It creates a global in a browser environment. It is nowhere near as controlled as using CommonJS modules.
If yes then what is the point of adding it in IIFE function which provides the feature of keeping everything private.
To keep everything else private and only expose the single variable.
Upvotes: 2