ASCII码 ASCII码

函数参数与返回值 |模板字面量与模板函数

发布于:2022-07-01 20:30:47  栏目:技术文档

函数参数与返回值 |模板字面量与模板函数

*函数的参数和返回值

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. let fn = (a, b) => a + b;
  13. console.log(fn(1, 5));
  14. // 参数不足 给参数默认值
  15. let f = (a, b = 1) => a * b;
  16. console.log(f(5));
  17. // 参数过多,用...arr接收
  18. f = (a, b, ...c) => console.log(a, b, c);
  19. console.log(f(1, 2, 3, 4, 5, 6));
  20. let arr = [1, 2, 3, 4, 5, 6];
  21. console.log(...arr);
  22. //返回多个值用数组或对象
  23. //数组
  24. let fnn = () => [1, 2, 3, 4];
  25. console.log(fnn());
  26. //对象
  27. fnnn = () => ({
  28. id: 1,
  29. name: "张老师",
  30. age: 33,
  31. });
  32. console.log(fnnn());
  33. </script>
  34. </body>
  35. </html>

*模板字面量与模板函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. console.log(`Hello world`);
  13. let name = "张老师";
  14. console.log(`Hello` + name);
  15. //也可以如下 用‘${xxxx}’表示占位符
  16. console.log(`Hello ${name}`);
  17. //也可以用表达式
  18. let age = 1;
  19. console.log(`${age ? `男:${name}` : `女`}`);
  20. //模板函数,用模板字面量当参数时 就是模板函数
  21. price`数量:${20}单价:${50}`;
  22. function price(strings, ...args) {
  23. console.log(strings);
  24. console.log(args);
  25. console.log(`总价:${args[0] * args[1]}元`);
  26. }
  27. </script>
  28. </body>
  29. </html>
相关推荐
阅读 +