1

I wrote the following Typescript code:

$('document').ready(() => {
   let fileSystem = new FileSystem(fileSystemInputs);
   $('#debug').click( () => {
      console.log(fileSystem);
      debug;
   });
}

I found out that once I click '#debug',the console prints the 'fileSystem' object. When I open the debugger (in Chrome developer tools), I can see 'fileSystem' object in the watch window. It's vary clear since it's valid inside the arrow function.

However, when I remove the line "console.log(fileSystem);" and the code now is only:

$('document').ready(() => {
   let fileSystem = new FileSystem(fileSystemInputs);
   $('#debug').click( () => {
      debug;
   });
}

once I open the watch window in the debugger, however, I see that 'fileSystem' is undefined. The line:

 console.log(fileSystem)

is not supposed to change the scope, so how is it possible that variables disappear?

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • 1
    This is just how the v8 engine behaves. You are correct, console.log isn't supposed to change the scope - but if v8 sees it doesn't have any reason to run your inside some other scope (and that scope runs under another scope until we reach the global scope), than v8 will run your function simply under the global scope. You can look here for more details http://stackoverflow.com/questions/28388530/why-does-chrome-debugger-think-closed-local-variable-is-undefined – Aviad Hadad Mar 01 '17 at 21:36

0 Answers0