Blogger Information
Blog 21
fans 0
comment 0
visits 10046
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1101实例演示JS中变量常量与函数的声明与使用过程
放手去爱
Original
417 people have browsed it
  1. // ! 函数
  2. // * 1. 命名函数
  3. // 调用: 声明前,成功了,说明函数声明提升到了代码顶部
  4. console.log(sum1(3, 4));
  5. // 声明
  6. function sum1(a, b) {
  7. return "a + b = " + (a + b);
  8. }
  9. // 调用: 声明后
  10. console.log(sum1(1, 2));
  11. console.log("-------------");
  12. // ? 必须遵循"先声明,后使用"原则,声明提升违背了该原则
  13. // * 2. 匿名函数
  14. // const ,let 没有声明提升的效果
  15. // console.log(sum2(7, 8))
  16. const sum2 = function (a, b) {
  17. return "a + b = " + (a + b);
  18. };
  19. console.log(sum2(5, 6));
  20. console.log("-------------");
  21. // 以后, 首选匿名函数
  22. // * 3. 箭头函数
  23. // 匿名函数的语法糖(简化)
  24. // 语法: 删除function, (...)=>{...}
  25. let sum3 = (a, b) => {
  26. return "a + b = " + (a + b);
  27. };
  28. console.log(sum3(9, 10));
  29. // 只有一条return ,可不写 {...}
  30. sum3 = (a, b) => "a + b = " + (a + b);
  31. console.log(sum3(10, 11));
  32. // 只有一个参数, (...)也可不写
  33. sum3 = (username) => "Hello, " + username;
  34. console.log(sum3("PHP学员"));
  35. // 没有参数, (...)必须写
  36. sum3 = () => "Hello, PHP学习者";
  37. // _ 也是一个合法变量标识符
  38. // sum3 = _ => 'Hello, PHP学习者'
  39. console.log(sum3());
  40. console.log("-------------");
  41. // ! 箭头函数与匿名函数的最大区别: 没有自己的this
  42. // * 4. 立即执行函数 (IIFE)
  43. // 一个语法,用 (...) 包住 就转为"表达式"
  44. let res = (function (a, b) {
  45. return "a + b = " + (a + b);
  46. })(60, 30);
  47. console.log(res);

Correcting teacher:PHPzPHPz

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