Thomas 'PointedEars' Lahn said the following on 2/3/2006 8:50 AM:
Wrong Thomas.
Let me quote what you ignored ok? Make sure you read it properly this time:
IN THE GLOBAL SCOPE.
Get it now? In the GLOBAL scope they are equivalent.
It has been proven before already that there can be an object in
the scope chain, before the global object, that is a host object.
Irrelevant to what I said.
This is the case in JScript as used in IE: if there is an HTML element with
name or ID of `myVariable' and `myVariable' has not been declared before,
this assignment results in an error everywhere in the code because this
host object implements the simple assignment operation differently than
specified in ECMAScript Edition 3 (which is nevertheless ECMAScript
compliant behavior.)
Yoohoo. Read what I wrote Thomas : "Unless you understand the
ramifications of not doing so".
Sidenote: I don't give a crap what ECMA says. I care what the browsers do.
Declaring this identifier as a variable identifier avoids this runtime
error because the scope chain is not used (global declaration) or used
differently (local declaration) then.
And if you are stupid enough to name your variables the same as your
ID's, then you deserve what you get. Using IE as a defense is no defense
at all.
And, please oh please name just one browser on the Web where the above
script won't create a Global Variable. Just one.
But, just in case you missed it again, let me repeat what I said one
more time:
In the GLOBAL SCOPE, there is no difference in the two script blocks.
Declare all your variables using the 'var' keyword *unless* you
understand the ramifications of not doing so.
Do you fail to understand what I wrote? You must.
Therefore, variables should be declared _always_ (or they may be no
variables at all.)
Again, you didn't understand what I wrote. Try it again.
Once again, you are wrong Thomas.
Which part of "almost" is unclear to you?
Let me repeat for you, again, what I wrote and I will endeavor to
explain it to you since you either ignored it or didn't comprehend it:
<quote>
Unless you know where to and where not to put them, you are safer not
putting them in.
</quote>
Now, you get an example that maybe will shed some light on what you are
missing.
<script type="text/javascript">
myNonVarDeclaredVariable = 0
for (var i=0;i<100;i++)
{
myNonVarDeclaredVariable++
}
alert(myNonVarDeclaredVariable)
</script>
Thats perfectly valid Script. It will do what it was designed to do.
Increment a variable. It does that. Now, someone comes along and reads
PointedEars saying "End your statements with ;" and they add semi-colons
to the ends of all the lines and get this:
<script type="text/javascript">
myNonVarDeclaredVariable = 0;
for (var i=0;i<100;i++);
{;
myNonVarDeclaredVariable++;
};
alert(myNonVarDeclaredVariable);
</script>
Now, by not understanding when to, and not to, add semi-colons the
unknowing just changed what the code does and introduced syntax errors.
So, "Unless you know where to and where not to put them, you are safer
not putting them in".
The JS Engine will parse the script block and insert them where they
belong and will not insert them where they do not belong.
When writing, it is better to be clear and concise. Your statement left
a lot of ambiguity, mine did not. Learn the difference.
Then, take your corrections like a man.