I think I know the answer but... is there any way to prevent a global variable from being modified by later-executing ? I know global variables ar
You can use closure technique, MYGLOBALS is an object that has a function called getValue against the "globals" associative array that is out of scope for everything except MYGLOBALS instance.
var MYGLOBALS = function() {
var globals = {
foo : "bar",
batz : "blah"
}
return { getValue : function(s) {
return globals[s];
}
}
}();
alert(MYGLOBALS.getValue("foo")); // returns "bar"
alert(MYGLOBALS.getValue("notthere")); // returns undefined
MYGLOBALS.globals["batz"] = 'hardeehar'; // this will throw an exception as it should