07.执行上下文.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>07.执行上下文</title>
  8. </head>
  9. <body>
  10. <!--
  11. 1、代码分类(根据位置来分)
  12. *全局代码
  13. *函数(局部)代码
  14. 2、全局执行上下文
  15. *在执行全局代码前将window确定为全局执行上下文
  16. *对全局数据进行预处理
  17. *var定义的全局变量==>undefined,添加为window的属性
  18. *function声明的全局函数==>赋值(fun),添加为window的方法
  19. *this==>赋值(window)
  20. *开始执行全局代码
  21. 3、函数执行上下文
  22. *在调用函数,准备执行函数体之前,创建对应的函数执行上下文对象(虚拟的,存在于栈中)
  23. *对局部数据进行预处理
  24. *形参变量==>赋值(实参)==>添加为执行上下文的属性
  25. *arguments==>赋值(实参列表),添加为执行上下文的属性
  26. *var定义的局部变量==>undefined,添加为执行上下文的属性
  27. *function声明的函数==>赋值(fun),添加为执行上下文的方法
  28. *this==>赋值(调用函数的对象)
  29. *开始执行函数体代码
  30. -->
  31. <script>
  32. //全局执行上下文
  33. console.log(a1,window.a1) //undefined undefined
  34. a2() //a2()
  35. console.log(this) //window
  36. var a1 =3
  37. function a2(){
  38. console.log('a2()')
  39. }
  40. console.log('-----------')
  41. //函数执行上下文
  42. function fn(a1){
  43. console.log(a1) //2
  44. console.log(a2) //undefined
  45. a3() //a3()
  46. console.log(this) //window
  47. console.log(arguments) //伪数组(2,3)
  48. var a2 = 3
  49. function a3(){
  50. console.log('a3()')
  51. }
  52. }
  53. fn(2,3)
  54. </script>
  55. </body>
  56. </html>