0

I was debugging an application when I decided I wanted to know what properties a higher level-scoped object had.

To my surprise chrome didn't even recognise the variable existed!
I've managed to boil the code down to this:

var a = 1;
var b = 2;
(function() {
    console.log(a);
    debugger;
})()

EDIT it seems the scenario doesn't work (that is..it works without issue) when the above code is just used on its own either in the console or in a html file. I observed the issue in a complex application (see screenshot below) and see it in the jsfiddle, but not in a codepen. If anyone could confirm I'm not crazy (other than that one coworker I used their machine to prove to myself) that'd be great

With the console open, have a look at the fiddle.

Once it pauses itself (due to the debugger breakpoint keyword) try to inspect b. It doesn't know it exists. But it knows a does, supposedly because it is mentioned in the immediate scope.

enter image description here

Firefox doesn't exhibit the same behaviour (when it's not busy misbehaving in its own way I discovered today..)

So what is going on?
All I can think of is v8 is optimising away references that aren't being used, but why would it do that?
I didn't think that would yield worthwhile performance gains.. Why does it do it even when the devtools are open, and is there a way to stop it?


Edit: nothing to do with fiddle or frames the app in question
I'm on Fedora running 46.0.2490.71 (64-bit) if that means anything

Community
  • 1
  • 1
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
  • yeah, jsfiddle doesn't work for any browser - as far as the firebug problem ... when firebug works, it's OK, when it doesn't it's a piece of crap on a cracker. firefox has built in dev tools that are far more robust than firbug – Jaromanda X Oct 22 '15 at 05:37
  • If you have trouble with jsfiddle, you can copy the code to any html file. Like I said, I was debugging an actual application when I came across this, so the issue isn't with fiddle; that's just the boiled down version for you guys – Hashbrown Oct 22 '15 at 05:39
  • I am unable to reproduce it! – Rayon Oct 22 '15 at 05:39
  • Tried on two machines; Linux x64 46.0.2490.71 – Hashbrown Oct 22 '15 at 05:40
  • I think it's more of a jsfiddle thing, have you tried opening a new tab and testing it there? – Griffith Oct 22 '15 at 05:41
  • I already mentioned the original problem was not a fiddle, I just wrote this snippet just then for this question specifically – Hashbrown Oct 22 '15 at 05:42
  • tried in a html with just that script - works fine in every browser I tried – Jaromanda X Oct 22 '15 at 05:43
  • well I did say it'd work in firefox but yeah, I just wrote it in [codepen](http://codepen.io/anon/pen/NGyqvg) for the others and `b` is referable, so I'll need to make something more behaviourably deterministic – Hashbrown Oct 22 '15 at 05:46
  • Typed it directly into the Chrome console and I see no problems. – Nocturno Oct 22 '15 at 05:46
  • The actual app is an angular app so it's hard to make something bigger than a tech demo like this. Out of curiosity, how many of you guys used the fiddle? Maybe optimisation is triggered by stupidly large code and that's what fiddle and the app have in common (there aren't any frames), whereas codepen and 'just typing it in [to a plain file/console]' is leaner – Hashbrown Oct 22 '15 at 05:50
  • You seem to be missing the fact that jsFiddle runs in multiple frames. Add a live console and it should work. – adeneo Oct 22 '15 at 05:50
  • http://jsfiddle.net/MWadX/123/ – adeneo Oct 22 '15 at 05:51
  • it may seem that way to you, but I'm seeing it in the live server environment @adeneo (see edit) – Hashbrown Oct 22 '15 at 05:55
  • @JaromandaX I was using default debugger, I don't have firebug, but yeah, firefox works fine – Hashbrown Oct 22 '15 at 05:57
  • 1
    In short, yes it is optimised away. Less for performance than for memory reasons I'd say. You can stop it by using `eval()`. For details, see the duplicate question and those linked from there. – Bergi Oct 22 '15 at 06:09
  • Thanks so much for taking the question seriously and not just blaming it on frames/jsfiddle et cetera. That does sorta answer my question (I'm just having trouble thinking why $httpProvider would have been cleaned up, or is it that the *reference* in this scope was GC'd?). I wish you had posted the question reference as an answer so I could comment and vote on it accordingly instead of in this massive comment thread – Hashbrown Oct 22 '15 at 06:20

0 Answers0