Re: IE javascript bug: global variable



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!
.



Relevant Pages

  • Re: pseudo-namespacing in JavaScript
    ... SCRIPT element the outcome becomes very different. ... assignment, and if the DIV element had been shown to proceed the SCRIPT ... execution context where the spec says it should not. ... that function declarations will replace the ...
    (comp.lang.javascript)
  • Re: [off topic]Strange Behaviour -- adding printf change the progra execution
    ... i just simply adding few debug like below and it changes program ... and a corresponding delay in the execution of other ... The problem is, if you've got a bug in your code, it has a pretty good ... source code, making it harder to detect the real defect. ...
    (comp.lang.c)
  • Addendum Re: Internet Explorer Pop-Up OBJECT Tag Bug
    ... adding an addendum about the bug which Dave ... able to control the execution of software). ... Download unsigned Activex controls - Disable ... Safe for Scripting - Disable ...
    (Bugtraq)
  • Go ahead. Stop programming. This ensures you from any mistakes.
    ... declarations, ... All normal languages obey it ... right away from simplicity. ... of instruction execution. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Switch statement help
    ... Also remember a very annoying cross-language bug in switch ... It's like having to put break after each function body so execution ... function bar() { ... maybe to have a flag to "continue" execution of underlaying branches - ...
    (comp.lang.javascript)

Loading