Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More scope example of a inner function
- var global_variable = 5;
- function simplefunc() {
- var local_variable = 6;
- function innerfunc() {
- console.log(global_variable);
- console.log(local_variable);
- }
- local_variable = 8;
- return innerfunc;
- }
- var f = simplefunc(); // innerfunc가 리턴됨
- f(); // prints '5', '8'
- // 위 예를 잘 이해했다면 '8'이 출력되는 이유를 설명할 수 있다.
- // innerfunc는 local_variable에 대한 레퍼런스를 가지고 있다.
- // 따라서 simplefunc에서 local_variable이 처음에는 6이었지만 8으로 변경된 것이
- // innerfunc가 가진 local_variable에 대한 참조에 그대로 반영되는 것은 자연스럽다.
Add Comment
Please, Sign In to add comment