Blogger Information
Blog 14
fans 0
comment 0
visits 23785
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演示JS中变量常量与函数的声明与使用过程
逍遥php
Original
520 people have browsed it

1.变量常量

1.1变量

  1. //1.变量
  2. //分号可选
  3. let username = "pavin";
  4. //2.函数
  5. function getUserName(username) {
  6. //函数体
  7. return "Hello," + username;
  8. }
  9. //调用函数
  10. console.log(getUserName(username));
  11. console.log("---------------------");
  12. // 1.变量:数据复用
  13. //2.函数:操作复用

1.2 常量

  1. //1.字面量
  2. "pavin", 100, [10, 20, 30], { x: 1, y: 2 }, function () {};
  3. //2.变量
  4. //声明+赋值(初始化)
  5. //let a
  6. //a=100
  7. let a = 100;
  8. console.log("a=", a);
  9. //更新(第二次及以上的赋值)
  10. a = 200;
  11. console.log("a=", a);
  12. console.log("--------------------");
  13. //3.常量 const
  14. const USER_NAME = "Pavin";
  15. console.log("USER_NAME=", USER_NAME);
  16. //常量禁止更新

2.函数的声明与使用过程

  1. //1.命名函数
  2. //声明
  3. function sum1(a, b) {
  4. return "a + b=" + (a + b);
  5. }
  6. //调用:声明后
  7. console.log(sum1(1, 2));
  8. //必须遵循"先声明,后使用"原则,声明提升违背了该原则
  9. console.log("------------------------");
  10. //2.匿名函数
  11. //const,let 没有声明提升的效果
  12. const sum2 = function (a, b) {
  13. return "a + b=" + (a + b);
  14. };
  15. console.log(sum2(3, 4));
  16. //以后,首选匿名函数
  17. console.log("----------------");
  18. //3.箭头函数
  19. //匿名函数的语法糖(简化)
  20. //语法:删除function,(...)=>{...}
  21. let sum3 = (a, b) => {
  22. return "a + b=" + (a + b);
  23. };
  24. console.log(sum3(5, 6));
  25. //只有一条return,可不写{....}
  26. sum3 = (a, b) => "a + b=" + (a + b);
  27. console.log(sum3(7, 8));
  28. // 只有一个参数,(....)也可不写
  29. sum3 = (username) => "hello," + username;
  30. console.log(sum3("Pavin"));
  31. //没有参数,(...)必须写
  32. sum3 = () => "hello,你好";
  33. console.log(sum3());
  34. //箭头函数与匿名函数的最大区别:没有自己的this
  35. console.log("----------------");
  36. //4.立即执行函数(IIFE)
  37. //一个语法,用(....)包住 就转为"表达式"
  38. let res = (function (a, b) {
  39. return "a + b=" + (a + b);
  40. })(9, 10);
  41. 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