I have a little bit of confusion understanding these module patterns what I understand is the 1st one is just like a class whenever you need it just assign to a new variable.
In 2nd one, we encapsulated our module we don't have access to globals except that we pass to the module as an argument, we also cannot pass any argument to the module when assigning to it variable like module(args) because it is an IIFE, if we want to use it as a class then we have to return the function not object.
In 3rd I think it just works like a singleton.
It would be also good to know the use cases for these patterns.
1
const module = function(args) {
const exports = {}
const privateMethod = () => {}
const exports.publicMethod = () => {}
return exports;
}
const instance = module(args)
2
const module = (function(globals) {
const exports = {}
const privateMethod = () => {}
const exports.publicMethod = () => {}
return exports;
})(globals)
const instance = module;
3
const module = module || {}
(function (module) {
const privateMethod = () => {}
module.publicMethod = () => {}
})(module)
module.publicMethod()
Aucun commentaire:
Enregistrer un commentaire