Consider the following code snippet:
<html><head></head> <body> <script type="text/javascript"> var outside_scope = "outside scope"; function f1() { alert(outside_scope) ; } f1(); </script> </body> </html>
The way out for this code is that the message "outside scope" is displayed in the warning field. But if I change the code a bit like:
<html><head></head> <body> <script type="text/javascript"> var outside_scope = "outside scope"; function f1() { alert(outside_scope) ; var outside_scope = "inside scope"; } f1(); </script> </body> </html>
the warning window displays the message "undefined". I could have understood the logic if it displays "undefined" in both cases. But then it doesn’t happen. It displays "undefined" only in the second case. Why is this?
Thanks in advance for your help!
javascript variables shadowing
BeachRunnerFred Oct 06 '09 at 21:57 2009-10-06 21:57
source share