javascript - Shouldn't this variable default to "top-level"? -


Currently the result is undefined and I do not understand why this is.

  var variable = "top-level"; Function parent function () {function childFunction () {warning (variable); } Childfunction (); Var variable = "local"; } Parent function ();   

I thought that because I declared a variable

  var vari = "local"; After I called the child function, it will look at the scope chain and instead will get top level variables, but it seems that this is not the case.  

This is due to the hosting, which is at the top of the var The statement is inserting, but still executes the assignment statement on that line. If you have a look below, then the second var variable will shade the global variable because it announces a local variable with the same name.

Actually, what your code does:

  var variable = "top-level"; Function parent function () {var variables; Function child function () {warning (variable); } Childfunction (); Variable = 'local'; } Parent function ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -