index.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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>箭头函数</title>
  8. </head>
  9. <body>
  10. <script>
  11. let fn = function(){
  12. }
  13. let fn = (a,b) =>{
  14. return a+b;
  15. }
  16. //调用
  17. let r = fn(3,4)
  18. console.log(r)
  19. //this是静态的,始终指向函数声明时所在的作用域下的this值
  20. function getName(){
  21. console.log(this.name)
  22. }
  23. let getName2 = () =>{
  24. console.log(this.name)
  25. }
  26. window.name = 'zhangsan'
  27. const school = {
  28. name = 'lisi'
  29. }
  30. getName() //zhangsan
  31. getName2() //zhangsan
  32. //call
  33. getName.call(school) //lisi
  34. getName2.call(school) //zhangsan 保持不变
  35. //不能作为构造函数实例化对象
  36. let Person = (name,age) =>{
  37. this.name = name;
  38. this.age = age;
  39. }
  40. let me = new Person('zhang',18)
  41. console.log(me) //报错
  42. //不能使用arguments变量
  43. let fn = () =>{
  44. console.log(arguments) // not defined
  45. }
  46. fn()
  47. //简写
  48. //省略小括号 当形参有且只有一个时
  49. let add = n =>{
  50. return n*n
  51. }
  52. //省略花括号 当代码只有一条语句时,return也必须省略
  53. let pow = (n) => n*n
  54. </script>
  55. </body>
  56. </html>