简体   繁体   中英

Global variable not passed down in stack - IE8

I am working on a legacy application that is breaking in Internet Explorer. It has the following code:

Script 1: /* Call script 2 */; xa(b) /* Call script 2 */; xa(b)

Script 2: eval(script3)

Script 3: var x = x || {}; xa = function(){ ... } var x = x || {}; xa = function(){ ... }

In Firefox, this works as expected.

In internet explorer, however, it fails by saying "x is undefined" (this is massively simplified). I traced it back to this point in the code and noted that x is defined in script 3 but then going back down in the stack to script 2 it loses its availability/scope (I tested this by putting console.log statements in both - IE was ok with the one in script 3 but crashed for the one in script 2).

What about internet explorer and/or the eval() function causes this to happen and what are my options for fixing it?

Why not set up a test and see for yourself?

Here is one: http://jsfiddle.net/Y3bf5/

It seems to work fine in IE7+.

You might want to change Script 3 to var x; x = x || {}; xa = function(){ ... } var x; x = x || {}; xa = function(){ ... }
This might've caused due to preemptive usage of the variable before it's been declared in this case variable x .
The fact that variable x is unavailable in Script 2 is because the eval() method failed abruptly with " x is undefined "
Actually I understood only a lil' bit from the piece of code that refers Script 3
If I could get a glimpse of it, then I might be of better help to you than now.
Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM