7.箭头函数.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. <style>
  8. #box {
  9. width: 300px;
  10. height: 300px;
  11. background: #00f;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box">
  17. <ul>
  18. <li>你好</li>
  19. <li>你好</li>
  20. <li>你好</li>
  21. </ul>
  22. </div>
  23. <script>
  24. /**
  25. * 箭头函数
  26. * 1.this指向
  27. * 2.不能作为构造函数
  28. * 3.没有arguments对象
  29. * 适用场景:
  30. * 1.回调函数(需要保留外层的this)
  31. * 2.定时器的回调
  32. * 3.Promise的回调
  33. */
  34. let fn3 = () => {
  35. console.log(this,'2')
  36. // console.log(arguments)
  37. };
  38. fn3();
  39. // let fn4 = new fn3();
  40. // console.log(fn4)
  41. // let fn1 = function() {
  42. // console.log(this,'1')
  43. // };
  44. // let fn2 = () => {
  45. // console.log(this,'2')
  46. // };
  47. // fn1();
  48. // fn2();
  49. // let box = document.getElementById("box");
  50. // let lis = document.querySelectorAll("li");
  51. // // box.onclick = () => {
  52. // // console.log(this,'3')
  53. // // }
  54. // for(let i=0;i<lis.length;i++) {
  55. // lis[i].onclick = () => {
  56. // console.log(this,'4')
  57. // }
  58. // }
  59. // function Person() {
  60. // this.name = "图图";
  61. // this.age = 3;
  62. // console.log(this,'5')
  63. // setTimeout(() => {
  64. // console.log(this,'6')
  65. // },1000)
  66. // }
  67. // let p = new Person();
  68. // console.log(p.name,p.age)
  69. </script>
  70. </body>
  71. </html>