3

I run in Chome devtools next code

(function() {
  var a = 5;
  debugger; // when I stop here I evaluate `a = 9`
  console.log(a);
})(); // and got 5

but if I use

(function() {
  var a = { a: 5 };
  debugger; // when I stop here I evaluate `a.a = 9`
  console.log(a.a);
})(); // and got 9

Why?

PS also why it doesn't work in FF / Safari (it even didn't stop in debugger line )

Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44
  • When running the second example i got 5 loggen in the console – Nick D Apr 18 '16 at 13:20
  • @NickD https://www.dropbox.com/s/yegaya6jgon6o1s/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202016-04-18%2016.34.07.jpg?dl=1 – Vasiliy vvscode Vanchuk Apr 18 '16 at 13:34
  • 2
    This seems to be issue https://bugs.chromium.org/p/chromium/issues/detail?id=124206 which was fixed on version 35 but regressed in version 49 (which I'm assuming is what you're using), and will be fixed in version 52. – apokryfos Apr 18 '16 at 13:38
  • In firefox only stops if the debugger is active, otherwise it doesn't stop. – Marcos Pérez Gude Apr 18 '16 at 13:57
  • @the `debugger;` appears to work fine for me in FF under that same criteria as Chrome (i.e., dev tools are open). If you open dev tools in FF, does the script pause on http://jsfiddle.net/t1mztbup/2/? – apsillers Apr 18 '16 at 17:26
  • @apsillers Yes, at your example it stops. But it doesn't if I run function with debugger right in console – Vasiliy vvscode Vanchuk Apr 19 '16 at 08:04

2 Answers2

1

This is behavior is simply a bug, and will be fixed in an upcoming release.

If you want a "why" deeper than that, you'll need to know a lot about Chrome's debugger and JavaScript implementation. According to the diff of one file in the fix, the debugger formerly used a context_builder.native_context but now it uses a context_builder.evaluation_context. Apparently the native_context created by the old debugger code had trouble resolving (or not treating as read-only) local-scope variables. If you really wanted more, you could contact the author of the fix.

As for why the debugger does not appear in Firefox: it will appear if you are running code from a <script> and have your dev tools open. When running code from the console, it appears that you must have the debugger tab open specifically. Obviously, this is not possible if you have the console open to type in your code, but you can wrap it in setTimeout and quickly switch to the Debugger tab:

setTimeout(function() { debugger; }, 5000)
apsillers
  • 112,806
  • 17
  • 235
  • 239
-2

It is a matter of how the variables are used. Objects are used by reference. So changing a.a will effectively change the value at the proper memory address. Though, changing a itself in any of your test version won't do anything because a new memory address is created for the variable evaluated in the console.

For FireFox not breaking at debugger line, it states in this page (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) : "If no debugging functionality is available, this statement has no effect.". So, you have to ensure FireBug is installed I presume.

Master DJon
  • 1,899
  • 2
  • 19
  • 30
  • To whom down voted this : http://stackoverflow.com/questions/639514/how-can-i-get-the-memory-address-of-a-javascript-variable Look at the answer of @user2310967 – Master DJon Apr 18 '16 at 13:46
  • As noted by [apokryfos's comment](http://stackoverflow.com/questions/36695126/why-chrome-dont-allow-change-variables-in-debug#comment60979279_36695126) this behavior [in Chrome is simply a bug](https://bugs.chromium.org/p/chromium/issues/detail?id=124206). Any explanation of Chrome's behavior here that does not note something like, "This behavior is a bug and should not manifest as it does currently," is incomplete. – apsillers Apr 18 '16 at 14:13
  • I can debug js in ff even without firebug. – Vasiliy vvscode Vanchuk Apr 18 '16 at 15:29
  • [The link you provided](http://stackoverflow.com/questions/639514/how-can-i-get-the-memory-address-of-a-javascript-variable) is not relevant, since console code runs in the current scope of the `debugger` statement. There is no reason why the local-scope variable `a` cannot be given a new value. If you equivalently replace the `debugger` statement with `a=9`, the code outputs `9` as expected. – apsillers Apr 18 '16 at 17:34
  • The OP demanded why it does that, not is a bug or not. The decision if it is a bug or not depends on what Google intended to do. More over, it explains that here, they could correct it by using the variables as references instead of value. – Master DJon Apr 18 '16 at 18:34
  • The answer of "why it does that" is "it is a bug" (as we can see by the developer's acknowledgement of the behavior as a bug on the linked bug report). That's it -- setting `a` to a value and then not seeing `a` change value is simply never correct JavaScript behavior (unless it's a `const` or is a property with `writeable: false`) I suppose when a JS implementation fails to satisfy such a fundamental required behavior, we can speculate about the root cause of that bug (as you do here?) but I'd argue that the primary answer is always, "It really shouldn't do this." – apsillers Apr 19 '16 at 12:44
  • Two ways of seeing it. In a chain of causes, you can decide the top one is the main one or the bottom (also called root) is the main one. – Master DJon Apr 19 '16 at 13:21