vendredi 24 novembre 2017

Private Variables in Module Pattern

I am not clear about the concept of private, if I can still access it through the public method and redefine the properties of the module. I mean, I can perfectly do:

var aModule = (function() {

    var privateVar = 1;

    return {
        publicFunction: function() {
            return privateVar;
        }
    }

})();

aModule.publicFunction = function() {
    privateVar = function() {
        console.log('a new content');
    }
    privateVar();
};

aModule.publicFunction(); // 'a new content'

I understand that this is not possible if I write it in ES6 with let or const, because it would give me error try to overwrite the value of the private variable, but what sense does it have in ES5?

Aucun commentaire:

Enregistrer un commentaire