Guest User

Untitled

a guest
Nov 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. // More scope example of a inner function
  2.  
  3. var global_variable = 5;
  4.  
  5. function simplefunc() {
  6. var local_variable = 6;
  7.  
  8. function innerfunc() {
  9. console.log(global_variable);
  10. console.log(local_variable);
  11. }
  12.  
  13. local_variable = 8;
  14. return innerfunc;
  15. }
  16.  
  17. var f = simplefunc(); // innerfunc가 리턴됨
  18.  
  19. f(); // prints '5', '8'
  20.  
  21. // 위 예를 잘 이해했다면 '8'이 출력되는 이유를 설명할 수 있다.
  22. // innerfunc는 local_variable에 대한 레퍼런스를 가지고 있다.
  23. // 따라서 simplefunc에서 local_variable이 처음에는 6이었지만 8으로 변경된 것이
  24. // innerfunc가 가진 local_variable에 대한 참조에 그대로 반영되는 것은 자연스럽다.
Add Comment
Please, Sign In to add comment