Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. // scope & scope chain commented
  2.  
  3. // was declared globally, outer reference is global
  4. function a() {
  5. // variable created inside context of a
  6. var myOtherVar = 'inside A'
  7.  
  8. // b called inside a, but was lexically declared in the global context
  9. b()
  10. }
  11.  
  12. // was declared globally, outer reference is global
  13. function b() {
  14. // variable exists in b
  15. var myVar = 'inside B'
  16.  
  17. // goes up to the global context, fetches myOtherVar and logs it
  18. console.log('myOtherVar:', myOtherVar)
  19.  
  20. // was declared inside b, outter reference is b
  21. function c() {
  22. // goes into the context of function b and logs myVar
  23. // it stops after finding the variable
  24. // meaning it does not go up again to the global scope unless it needs to
  25. console.log('myVar:', myVar)
  26. }
  27.  
  28. c()
  29. }
  30.  
  31. // global variables
  32. var myOtherVar = 'global otherVar'
  33. var myVar = 'global myVar'
  34.  
  35. a()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement