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
try this:
const whatEver = 'Hello World!!!';
function foo(value){
whatEver = value;
}
then you would call it like so...
<div onclick="foo('New Value');">Change Me First</div>
<div onclick="alert(whatEver);">Then click me After: Should Be alert "Hello World!!!"</div>