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()();
?