My understanding of revealing module pattern is as follows:
CODE #1
const iifeModulePattern = (function () {
const secretSeed = 999; // ← private
const logSecretCode = function(){console.log(secretSeed+1)};
return {methodForPublic : logSecretCode};
})();
iifeModulePattern.methodForPublic();
So far I hope I’m not wrong. My question is:
- Won’t the following Code #2 serve same purpose?
- If it does, why is Code #1 popular than Code #2?
- If it doesn’t, what’s the difference?
CODE #2
const modulePattern = () => {
const secretSeed = 999; // ← private
const logSecretCode = function(){console.log(secretSeed+1)};
return {methodForPublic : logSecretCode};
};
modulePattern().methodForPublic();
I won’t store “secret” codes (like passwords) in this way. The above codes are just examples.