Blogger Information
Blog 56
fans 1
comment 0
visits 62258
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ES6箭头函数的常用场景与类应用场景及作用
零龙
Original
1192 people have browsed it

ES6 箭头函数

  • 箭头函数提供了一种更加简洁的函数书写方式

  • 基本用法及ES6类的申明

  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>箭头函数</title>
  7. <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
  8. </head>
  9. <style>
  10. .like{ font-size:35px; color:#ccc; cursor:pointer;}
  11. .cs{ font-size:35px;color:#f00;cursor:pointer;}
  12. span{font-size: 12px; line-height: 50%;}
  13. </style>
  14. <span class="like" id="dian">&#10084;</span>
  15. <span id="zang"></span>
  16. <body>
  17. <script>
  18. // 1.无参数的箭头函数
  19. let a1 = () => {};
  20. console.dir(a1());
  21. // 2.传参数的箭头函数
  22. let a2 = (itme) => console.log(`->a2(${itme})`);
  23. a2(25);
  24. //3.多个参数
  25. let a3 = (...items) => console.log(`->a3(${items})`);
  26. a3(1,2,3,4);
  27. //4,箭头函数的应用
  28. let a4 = (a,b) => console.log(a + b);
  29. a4(5,8);
  30. //箭头函数的累加
  31. let a5 = (...args) => args.reduce((prev,next) => prev + next,0);
  32. //reduce函数相加后的总和
  33. console.log(a5(9,33,44,12,5,66,44,33));
  34. // 箭头函数的遍历
  35. let a6 = (...items) => items.map((item)=>console.log(item));
  36. a6("曹操","刘备","董卓");
  37. let a7 = (d,l,m) => console.log(d+l+m);
  38. a7(5,5,3);
  39. a7(...[8,4,6])
  40. let a8 = (w,...items) => console.log(w,items);
  41. a8(5,6,7,8);
  42. a8(...[5,6,7],8);
  43. //5.属性的简化
  44. let a9 = (id,name,email) => ({id,name,email});
  45. user = a9(1,"曹操","cc@qq.com");
  46. console.log(user);
  47. //6.方法的简化
  48. let a10 = {
  49. sName : "三国演义",
  50. getSg()
  51. {
  52. return this.sName;
  53. },
  54. };
  55. console.log(a10);
  56. //7.闭包应用场景
  57. let a11 =() =>
  58. {
  59. let username = "曹操";
  60. let get = () =>username;
  61. return get;
  62. }
  63. let a12 = a11();
  64. console.log(a12());
  65. //子函数调用父函数的变量值
  66. //8.创建多个全局可用的API
  67. let set,inc,dec,print
  68. let manager = (n) =>
  69. {
  70. let num = n;
  71. //定义一个变量接收数值
  72. set = (val) => {num = val};
  73. //存储num
  74. inc = () => { num++; return num;}
  75. dec = () => { num--; return num;}
  76. print = () => { return num;} ;
  77. };
  78. manager(10);
  79. document.getElementById("zang").innerHTML = print();
  80. $(function () {
  81. $("#dian").click(function () {
  82. if(document.getElementById("dian").className==='like')
  83. {
  84. document.getElementById("dian").className = 'cs';
  85. document.getElementById("zang").innerHTML = inc();
  86. }
  87. else
  88. {
  89. document.getElementById("dian").className = 'like';
  90. document.getElementById("zang").innerHTML = dec();
  91. }
  92. });
  93. });
  94. //9.ES6 类的申明
  95. class UserName
  96. {
  97. constructor(name)
  98. {
  99. this.name = name;
  100. }
  101. setname()
  102. {
  103. return this.name;
  104. }
  105. }
  106. username = new UserName("刘备");
  107. console.log(username.setname());
  108. </script>
  109. </body>
  110. </html>

示例图:

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:箭头函数其实并不是什么新发现, python中早以有之, 所以抽空多学几门编程语言没有错的, 不少语言之间的语法中相互借鉴的
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post