Re: IE javascript bug: global variable
- From: mikewse@xxxxxxxxxxx
- Date: Mon, 18 Feb 2008 05:59:27 -0800 (PST)
On 18 Feb, 13:28, Henry <rcornf...@xxxxxxxxxxxxxx> wrote:
On Feb 18, 11:56 am, mike...@xxxxxxxxxxx wrote:
The first block assigns a global
variable though "window.myObj" syntax.
Strictly that is not a global variable; it is a property of the global
object. Global variable declarations result in properties of the
global object with the internal - DontDelete - attribute set, while
properties of the global object created through assignment can be
deleted.
Ah, thanks, I didn't realize there was a difference between these two
ways of creating a "global variable" (property of the global object)
and have been using them interchangibly.
<script type='text/javascript'>
window.myObj = {};
alert( myObj );
</script>
<script type='text/javascript'>
alert( myObj );
// the following line causes myObj == undefined !
// if (myObj == null) var myObj = {};
</script>
Presumably you are unaware that in javascript all variable
declarations and function declarations are acted upon during variable
instantiation for the pertinent execution context and so precede the
actual execution of code in that context.
I was actually aware of this, and among other tests have seen that
removing "var" (leaving just the assignment) also works around the
bug.
But with a correct/"bug-free" implementation I should be able to use
the global.property and var syntaxes interchangibly, right?
Yes that is a bug.<snip>
Then again ECMAScript was not really defined with
sequences of separate global execution contexts in mind
That's another aspect of this bug that puzzles me; all global code in
the first block should "run to completion" before the second block is
entered. Specifically, both variable instantiation and actual code
execution in the first block should come before variable instantiation
of the second block. But parts of this bug makes me think that IE runs
some part of the variable instantiation in the second block before
code execution in the first block as it comes to the resolution that
the variable needs to be initialized.
This is contradicted though by testing to merge the two script blocks;
the bug doesn't show itself in this case.
Declare all of the global variable you are going to use prior to
referencing them and you won't have any issues resulting from this bug.
The scenario is more complex than outlined as I just provided the
simplest possible code to describe the bug. Specifically, it's about
nested variables x.y.z (hindering the use of var declarations) and the
inclusion of separate script files (therefore the two script blocks).
But with knowledge of the bug's anatomy it will be possible to work
around it.
Thanks for your help!
.
- Follow-Ups:
- Re: IE javascript bug: global variable
- From: Henry
- Re: IE javascript bug: global variable
- From: Doug Gunnoe
- Re: IE javascript bug: global variable
- References:
- IE javascript bug: global variable
- From: mikewse
- Re: IE javascript bug: global variable
- From: Henry
- IE javascript bug: global variable
- Prev by Date: Re: Prototype - Good/Bad/Why?
- Next by Date: formatting date/time correctly for If-Modified-Since during AJAX call
- Previous by thread: Re: IE javascript bug: global variable
- Next by thread: Re: IE javascript bug: global variable
- Index(es):
Relevant Pages
|
Loading