Steps:
Open page in Firefox 29.0.1 (Windows 7 x64):
<!doctype html> <title>Test</title> <script> (function () { var x = 5, f = function () { setTimeout(f, 1000); }; f(); }()); </script>
Open Developer Tools (F12).
In the Debugger, set a breakpoint on:
setTimeout(f, 1000);
Once the breakpoint is hit, evaluate
x
in the console. The result:undefined
Reload the page. The breakpoint is hit on the first run of
f
.Evaluate
x
. The result:5
Resume execution, and when the breakpoint is hit again, evaluate
x
. Same result:5
My assumption: If Firefox realizes on the first run of f
that x
is not needed, then it doesn't store the value of x
"with" f
. So in subsequent calls to f
the value of x
is undefined
. Interestingly, I see the same behavior in Chrome 35 and IE11.
Question: What is going on? Can I configure Firefox to make x
evaluate to its correct value in step 4 (see above)?
Steps:
Open page in Firefox 29.0.1 (Windows 7 x64):
<!doctype html> <title>Test</title> <script> (function () { var x = 5, f = function () { setTimeout(f, 1000); }; f(); }()); </script>
Open Developer Tools (F12).
In the Debugger, set a breakpoint on:
setTimeout(f, 1000);
Once the breakpoint is hit, evaluate
x
in the console. The result:undefined
Reload the page. The breakpoint is hit on the first run of
f
.Evaluate
x
. The result:5
Resume execution, and when the breakpoint is hit again, evaluate
x
. Same result:5
My assumption: If Firefox realizes on the first run of f
that x
is not needed, then it doesn't store the value of x
"with" f
. So in subsequent calls to f
the value of x
is undefined
. Interestingly, I see the same behavior in Chrome 35 and IE11.
Question: What is going on? Can I configure Firefox to make x
evaluate to its correct value in step 4 (see above)?
-
3
x
only exists within your function and you're trying to access it from outside. – Meredith Commented Jun 5, 2014 at 20:34 - I'm not getting this issue. When I step into it, it's evaluating correctly. Fox 23.0.1 built in dev tools. I've experience the debugger not working correctly if you load a page, start debugger, and try to debug from there. I used to have to load the page, load debugger, refresh page to get it to work. – Joe Swindell Commented Jun 5, 2014 at 20:38
-
@Meredith I'm accessing
x
from the breakpoint onsetTimeout(f, 1000);
, which is inside of the function. – feklee Commented Jun 5, 2014 at 20:42 - @JoeSwindell I am actually talking about the built in development tools, not about Firebug. Sorry for the confusion, I got the name wrong. Anyhow, reloading the page with development tools open doesn't solve it for me. To trigger the issue, it is important not to have the breakpoint set when loading the page. – feklee Commented Jun 5, 2014 at 20:48
- @feklee Without the breakpoint, you're trying to access x from outside a scope where it's defined. The function() wrapper makes sure that x is not defined in the main scope of the page. If you removed the outer function wrapper, x would be defined. – Patrick Gunderson Commented Jun 5, 2014 at 21:37
1 Answer
Reset to default 6It seems your assumption is correct. The problem is that the variable has been optimised out or removed when the debugger wasn't running. When you refresh the page with the debugger running, it alllows you to access the inner scopes of functions so that you can debug your code more easily.
This wouldn't be possible if there wasn't a breakpoint there, so the JIT piler has to be disabled to let you do so. The garbage collector also notes that these can still be referenced in-scope, so doesn't remove these variables.
If the JIT piler was running, it would recognise that the x
variable is not used anywhere and remove that from the code generated. If the JIT piler was not running, garbage collection (GC) would remove the variable when the GC cycle runs, as there is nothing referencing the variable.
Because it can't undo what it's already done, it is undefined when you hit the breakpoint the first time.
You can't really prevent this occuring, even if you disable the JIT piler. While technically you could set the garbage collection limits really high to try and stop it doing so or even remove the garbage collection code and rebuild Firefox, that's pretty stupid -- you'd just end up leaking a bunch of memory and that'd be much more hassle than simply refreshing the page.
Interestingly though, Firefox 31 (Aurora) provides a more descriptive error message if you try and do the same thing:
Error: variable has been optimized out
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744144614a4560361.html
评论列表(0条)