Those of us familiar with javascript know we should not be polluting the global scope with variables, I'm just wondering what the best method is as I use quite a few in various places, all seem to work but I'd be interested to know what others think is the better method.
1. Using an object with the function as a method.
var scope1 = {
init: function(){
// Variables here are only scoped to this objects init method
}
};
scope1.init();
2. using a declared function
function scope2(){
// variables here are scoped to this function only
}
scope2();
3. using an immediatley invoked function expression (IFEE)
(function scope3() {
// variables here closed to this function scope.
}());
1. Using an object with the function as a method.
var scope1 = {
init: function(){
// Variables here are only scoped to this objects init method
}
};
scope1.init();
2. using a declared function
function scope2(){
// variables here are scoped to this function only
}
scope2();
3. using an immediatley invoked function expression (IFEE)
(function scope3() {
// variables here closed to this function scope.
}());