I'm reading this article about the Javascript Module Pattern and came across a snippet which claims you can preserve private variables/functions across module files and access them from each other:
var MODULE = (function (my) {
var _private = my._private = my._private || {},
_seal = my._seal = my._seal || function () {
delete my._private;
delete my._seal;
delete my._unseal;
},
_unseal = my._unseal = my._unseal || function () {
my._private = _private;
my._seal = _seal;
my._unseal = _unseal;
};
// permanent access to _private, _seal, and _unseal
return my;
}(MODULE || {}));
I'm a little confused as to how I go about implementing this, but I think most of that confusion is from not understanding quite how this works; how does it enable other scripts already loaded to utilize those internal values? And how do I add functions and variables within this 'private' space?
My end goal is to provide a sort of 'abstract' function variable that must be overridden in sub-modules, but still needs referenced in the base module's file. Admittedly I haven't played with this much but that's mostly because I'm trying to wrap my head around how the actual implementation works.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire