练习题1_讲解.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 第一题
  11. // var n = 13;
  12. // function fn(n) {
  13. // alert(n);//13
  14. // n = 14;
  15. // alert(n);//14
  16. // }
  17. // fn(n);
  18. // alert(n);//13
  19. // var n = 13;
  20. // function fn(n) {
  21. // console.log(n);
  22. // var n = 6;
  23. // console.log(n);
  24. // }
  25. // fn(5);
  26. // var x = 13;
  27. // function fn(n) {
  28. // console.log(n);
  29. // x = 6;
  30. // console.log(x);
  31. // }
  32. // console.log(x);
  33. // fn(5);
  34. // console.log(x);
  35. // 第二题
  36. // 作用域链 当发现有变量调用的时候 先从当前作用域查找 如果没在上一层作用域查找 如果还没有再向上一层逐层查找
  37. // var n = 0;
  38. // function a() {
  39. // var n = 10;
  40. // function b() {
  41. // n++;
  42. // alert(n);
  43. // }
  44. // b();
  45. // }
  46. // a();
  47. // alert(n);
  48. // 第三题
  49. // console.log(num, str);//undefined undefined
  50. // var num = 18;
  51. // var str = "lily";
  52. // function fn2() {
  53. // console.log(str, num);//lily undefined
  54. // num = 19;
  55. // str = "candy";
  56. // var num = 14;
  57. // console.log(str, num);//candy 14
  58. // }
  59. // fn2();
  60. // console.log(str, num);//candy 18
  61. // 第四题
  62. // fn();//2
  63. // function fn() { console.log(1) };
  64. // fn();//2
  65. // var fn = 13;
  66. // fn();//报错 因为fn已经被赋值为13了 所以不是函数了
  67. // function fn() { console.log(2) };
  68. // fn();//报错 因为fn已经被赋值为13了 所以不是函数了
  69. // 第五题
  70. // (function f() {
  71. // function f() { console.log(1) };
  72. // f();
  73. // function f() { console.log(2) };
  74. // })();
  75. // var foo = function(){
  76. // console.log("hello");
  77. // }
  78. // foo();
  79. // 立即执行函数
  80. // (function(){
  81. // console.log("hello");
  82. // })()
  83. // 第六题
  84. // var a;
  85. // console.log(window.a);
  86. // if (!("a" in window)) {
  87. // var a = 10;
  88. // }
  89. // alert(a);
  90. // 函数提升 在大括号中 函数声明会被提升到作用域的顶部不包含函数体
  91. // console.log(fn);
  92. // if (8 == 8) {
  93. // function fn() {
  94. // alert(2);
  95. // }
  96. // }
  97. // var a = 10;
  98. // console.log(window.a);
  99. // in 运算符 可以判断一个属性是否在对象中
  100. // 如果在返回true 不在返回false
  101. // var obj = {
  102. // a:1,
  103. // b:2
  104. // }
  105. // console.log("c" in obj);
  106. // 第七题
  107. function fn() {
  108. var i = 5;
  109. return function (n) {
  110. console.log(n * i++);
  111. }
  112. }
  113. var f = fn();
  114. f(4);
  115. fn()(5);
  116. f(6);
  117. </script>
  118. </body>
  119. </html>