Last active
October 15, 2016 00:28
-
-
Save hackjutsu/6c943f91e08316071bc667d72b74b94f to your computer and use it in GitHub Desktop.
Example explaining the lexical scope in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var a = 10; | |
| function add() { | |
| var b = 20; | |
| return a + b; // a is bound to the global a when the function object add() is created. | |
| } | |
| // call add() -> 30 | |
| (function() { | |
| var a = 20; | |
| var result = add(); | |
| console.log("result: " + result); | |
| }()) | |
| // result: 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment