Blogger Information
Blog 15
fans 0
comment 0
visits 9228
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0104 函数练习
fanyigle_php
Original
586 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. // 函数声明与调用:
  12. // 1.标准声明,不能重申明
  13. function myFunc() {
  14. return "hello!";
  15. }
  16. console.log(myFunc());
  17. // 2.匿名函数
  18. // 套个马甲名字,找到入口
  19. let newFunc = function () {
  20. return "hello again";
  21. };
  22. newFunc = function () {
  23. return "hello again!!";
  24. }; //利用变量的let 和 const关键字可以反复申明为不同值;
  25. console.log(newFunc()); //函数调用时必有括号,不加即它本身;
  26. // 3.匿名函数=>箭头函数
  27. // 箭头函数怎么缩写都少不了箭头=>
  28. newFunc = (a, b) => a + b; // 箭头函数的参数定义可以是0个带括号,1个可带可不带括号,2个及以上带括号,返回语句如果只一行可以省掉花括号;
  29. console.log(newFunc(1, 2));
  30. // 4.函数的参数定义和传入参数可以是数组,也可以是函数
  31. asum = function (...arr) {
  32. //...把数组拆解
  33. console.log(arr);
  34. return arr.reduce(function (p, c) {
  35. return p + c;
  36. });
  37. };
  38. console.log(asum(1, 2, 3, 4));
  39. let params = [15, 20, 35, 40];
  40. console.log(asum(...params)); //...把数组拆解
  41. // 传入参数是个函数
  42. document.addEventListener("click", function () {
  43. alert("Hello World~~");
  44. });
  45. // 5.函数的返回值可以是函数等值类型,哪里调用哪里返回,问题是谁去接住,或者需不需要返回;
  46. // 偏函数: 简化了声明时的参数声明
  47. let sum = function (a, b) {
  48. return function (c, d) {
  49. return a + b + c + d; // c,d是局部变量,a,b既不是全局变量又不能undefined所以就跨调用留存;
  50. };
  51. };
  52. let f1 = sum(1, 2);
  53. console.log(f1(3, 4));
  54. // 柯里化
  55. sum = function (a) {
  56. return function (b) {
  57. return function (c) {
  58. return function (d) {
  59. return a + b + c + d;
  60. };
  61. };
  62. };
  63. };
  64. // 简化了调用参数
  65. let res = sum(10)(20)(30)(40);
  66. console.log(res);
  67. </script>
  68. </body>
  69. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
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