7.箭头函数.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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: 200px;
  10. height: 200px;
  11. background: aqua;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box"></div>
  17. <script>
  18. /**
  19. * 箭头函数
  20. *xxx = () => {
  21. * }
  22. * 1.箭头函数中没有this this指向上级
  23. * 2.箭头函数中不能使用arguments 可以使用rest
  24. * 3.箭头函数不用return
  25. * 4.箭头函数无法使用new
  26. */
  27. // box.onclick = () =>{
  28. // }
  29. // var box = document.getElementById("box");
  30. // box.onclick = function() {
  31. // console.log(this,'普通函数')
  32. // }
  33. // let a ='';
  34. // box.onclick = function(){
  35. // console.log(this,'1')
  36. // setInterval(()=> {
  37. // // a ="哈哈";
  38. // console.log(this,'监听函数')
  39. // // return a;
  40. // },1000)
  41. // }
  42. // function fn4() {
  43. // return a = '哈哈'
  44. // }
  45. // fn4()
  46. // console.log(a);
  47. // function fn1(){
  48. // console.log(this,'普通函数')
  49. // }
  50. // fn1();
  51. // let fn2 = ()=>{
  52. // console.log(this,'箭头函数')
  53. // }
  54. // fn2()
  55. let fn3 = (...rest) =>{
  56. console.log(...rest)
  57. // console.log(arguments)
  58. }
  59. // new
  60. fn3(1,2,3,4,5,7)
  61. </script>
  62. </body>
  63. </html>