Blogger Information
Blog 13
fans 0
comment 0
visits 6808
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
流程控制语句、函数的参数与返回值、模板字符串与模板函数
云中
Original
415 people have browsed it

流程控制语句

  1. // 单分支
  2. let a = 10;
  3. if (a < 20) {
  4. console.log(a);
  5. }
  6. // 双分支
  7. if (a > 20) {
  8. console.log(a);
  9. } else {
  10. console.log("小于等于20");
  11. }
  12. // 多分支
  13. if (a > 20) {
  14. console.log("大于20");
  15. } else if (a > 15) {
  16. console.log("大于15");
  17. } else if (a > 10) {
  18. console.log("大于10");
  19. } else {
  20. console.log("小于等于10");
  21. }
  22. a = 15;
  23. switch (true) {
  24. case a > 20:
  25. console.log("大于20");
  26. break;
  27. case a > 15:
  28. console.log("大于15");
  29. break;
  30. case a > 10:
  31. console.log("大于10");
  32. break;
  33. default:
  34. console.log("小于等于10");
  35. }
  36. switch (a) {
  37. case 15:
  38. console.log(a);
  39. break;
  40. case 20:
  41. console.log(20);
  42. break;
  43. default:
  44. console.log("object");
  45. }
  46. // 循环
  47. let i = 0;
  48. while (i < 20) {
  49. console.log(i);
  50. i++;
  51. }
  52. do {
  53. console.log(i);
  54. i++;
  55. } while (i < 10);
  56. let s = [1, 2, 3];
  57. for (let j = 0, k = s.length; j < k; j++) {
  58. console.log(j);
  59. }
  60. // for - of 迭代集合元素
  61. const lang = ["css", "html", "js"];
  62. // 输出数组中的 "键值" 对组成的数组
  63. for (let item of lang.entries()) {
  64. console.log(item);
  65. }
  66. // 输出 键
  67. for (let item of lang.keys()) {
  68. console.log(item);
  69. }
  70. // 输出 值
  71. for (let item of lang.values()) {
  72. console.log(item);
  73. }
  74. // 简化方法
  75. for (let item of lang) {
  76. console.log(item);
  77. }
  78. // for - in 遍历对象元素
  79. cl = { a: 1, b: 2, c: function () {} };
  80. for (let i in cl) {
  81. console.log(cl[i]);
  82. }

函数的参数与返回值

  1. // 函数的参数
  2. let f = (a, b) => a * b;
  3. console.log(f(1, 2));
  4. // 默认参数,放最后
  5. f = (a, b = 1) => a * b;
  6. console.log(f(3));
  7. let s;
  8. //将所有变量,压入一个数组
  9. f = (...par) => (s = par);
  10. console.log(f(1, 2, 4));
  11. // 展开数组
  12. console.log(...s);
  13. // 将多余的参数压入数组c中
  14. let ff = (a, b, ...c) => `${c}`;
  15. console.log(ff(1, 2, 3, 4, 5, 6));
  16. // 返回值
  17. // 返回数组
  18. let r = () => [1, 2, 3];
  19. console.log(r());
  20. // 返回对象
  21. r = () => ({ a: 1, b: [1, 2, 3], c: function () {} });
  22. console.log(r());

模板字符串与模板函数

  1. // 模板字符串
  2. let p = "html";
  3. console.log(`${p} 语言`);
  4. // 模板字符串可以计算
  5. console.log(`${1 + 2}`);
  6. // 模板字符串可以填表达式
  7. console.log(`${1 > 2 ? "大" : "小"}`);
  8. // 模板函数
  9. // 就是使用模板字符串为参数的函数
  10. // 模板字符串必须是第一个变量
  11. function f(mu, a1, a2) {
  12. console.log(mu);
  13. console.log(a1);
  14. console.log(a2);
  15. }
  16. // 将模板字符串中的插值作为参数传递给函数
  17. f`这是模板字符串 ${10}${20}`;
  18. // 优化写法
  19. function ff(mu, ...a) {
  20. console.log(mu);
  21. console.log(a);
  22. }
  23. ff`这是优化写法 ${10}${20}${30}`;
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