I had to use json2.js in my project as browser(IE8) JSON object was not available for parsing strings to JSON.
I ran through json2.js and am having a doubt with the variable declaration.
A JSON global variable is declared in json2.js like
var JSON;
if(!JSON){
JSON={};
}
What is the effect of declaration var JSON; on the global JSON object.
I hope the declaration should override the global JSON object in any browser (IE8/IE7).
But for my surprise it is not overriding when a global object is available.
Only a variable definition / initiation overrides a global variable?
Kindly clarify.
For each variable declaration (not initialization!). The following happens (section
#10.5):So you see, whenever
var xis encountered, it is tested whether a variable with namexalready exists in the environment. If yes, it is just ignored, but if not, then the variable is declared and initialized withundefined.Since the code is run in global scope it tests whether
JSONexists in global scope. So ifJSONalready exists,var JSON;is just ignored.Just some thoughts regarding testing/explaining this behaviour:
I don’t know at which point in the JavaScript execution the global object is created, but I assume before all other scripts are evaluated. That means,
JSONexists and has a value before any variable declaration, something you can only simulate if you include two scripts (can also be inline I guess, they are evaluated after another).Try:
What’s the result? (cheaters look here).
Whenever you are in a single script file, all variable declarations are hoisted to the top anyways. So if you have
the script is actually executed as:
You could not actually test whether the second
var foo;overwrites the first one, since at this point it has no value yet. So this is not a good example to demonstrate the behaviour quoted above.