svdsvd
svdsvd

Reputation: 37

Ways to protect the global scope in Javascript

ORIGINAL QUESTION:

i am studying js and i would like to know if there is any (useful) difference between these two ways of protecting the global scope other than the fact that the second one can be reused (called again).

option1:

var question = {};
(function(){ 
    question.option1 = function() {
        // some code
    };
})();

option2:

var question = {};
question.option2 = function () {
    //some code
};
question.option();

thanks!

EDIT 1:

thank you @luisperezphd. is there any difference between what you wrote and this (besides verbosity)?

var question = {};
question.option3 = {};
question.option3.privateVar = 0;
question.option3.testing = function () {
    question.option3.privateVar++;
    // some code
};
question.option3.testing();

EDIT 2:

thank you lanston and luisperezphd! i did not realize that question.option3.privateVar was available in the global.

is there any difference between this:

var question = {};
(function(){
    var privateVar = "some value";
    question.option4 = function(){
        alert(privateVar);
    }
})();
question.option4();

and this:

var question = {};
question.option5 = function() {
    var privateVar = "some value";
    var someFunction = function() { 
        alert(privateVar);
    }
    return someFunction;
}
question.option5()();

?

Upvotes: 3

Views: 2142

Answers (2)

Luis Perez
Luis Perez

Reputation: 28120

There is no difference in the example you gave, but there is a difference you haven't exploited. In the first example, you can utilize variables and not pollute the global namespace or the object namespace. The equivalent of a private field in most object orientated languages. You would do that like so:

var question = {};
(function(){ 
    var PrivateVariable = 0;
    question.option1 = function() {
        PrivateVariable++;
        // some code
    };
})();

alert(question.PrivateVariable); // returns 'undefined'

The reason the code above returns undefined is because PrivateVariable is not a field in question. But functions in question can access PrivateVariable. This is truly a private variable.

On the other hand, if you wrote it like this:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    question.PrivateVariable++;
    // some code
};

alert(question.PrivateVariable); // returns 0

In this second case, PrivateVariable is in fact not private and publicly accessible.

Incidentally, you normally wouldn't reference question from inside a function that belongs you question. Instead, you would use the this keyword, like so:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    this.PrivateVariable++;
    // some code
};

But this would only work on public variables. It makes it more clear what is going on. Also, in some cases, it makes maintaining the code easier in that if you change the name of the variable from question you wouldn't have to change references to it inside the function. There are other benefits, but I don't know if I should get into it here.

Upvotes: 4

Lanston
Lanston

Reputation: 11854

@svdsvd,It's quite different between liusperezphd's and yours, the PrivateVariable,you can't get it in the global,but you can get your question.option3.privateVar in the global

Upvotes: 0

Related Questions