Blogger Information
Blog 33
fans 0
comment 0
visits 17126
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数,返回值与模板字面量,模板函数
lucaslwk
Original
347 people have browsed it

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

一.函数参数与返回值

  1. let f1 = (a, b) => a * b;
  2. //1.参数个数正常
  3. console.log(f1(2, 3));
  4. //2.参数不足
  5. console.log(f1(4));
  6. //NaN,非数值,not a number
  7. //解决办法,默认参数
  8. f1 = (a = 0, b = 1) => a * b;
  9. console.log(f1(4));
  10. //3.参数过多
  11. console.log(f1(5, 6, 7, 8));
  12. //无法将全部的参数接收
  13. //解决办法,剩余参数...
  14. f1 = function (a = 0, b = 1, ...c) {
  15. console.log(c);
  16. return a * b;
  17. };
  18. console.log(f1(5, 6, 7, 8));
  19. //函数默认单值返回
  20. let f2 = (a, b) => a - b;
  21. console.log(f2(9, 10));
  22. //可以通过数组或者对象返回多个值
  23. f2 = (a, b) => [a, b, a - b];
  24. console.log(f2(9, 10));
  25. f2 = (a, b) => ({
  26. firstNum: a,
  27. lastNum: b,
  28. minus: a - b,
  29. });
  30. console.log(f2(9, 10));

二.模板字面量与模板函数

  1. //对象字面量的简化
  2. //变量名与属性名相同时,且处于相同的作用于中,可以不写变量名
  3. let teacher1 = {
  4. class: "一班",
  5. lesson: "语文",
  6. };
  7. console.log(teacher1.class);
  8. //简写
  9. let class1 = "一班";
  10. let teacher2 = {
  11. class1,
  12. lesson: "数学",
  13. };
  14. console.log(teacher2.class1);
  15. let lesson = "英语";
  16. //对象方法的简化
  17. let teacher = {
  18. class1,
  19. lesson,
  20. who() {
  21. return `${class1}${lesson}老师`;
  22. },
  23. };
  24. console.log(teacher.who());
  25. //模板字面量,可以使用插值表达式的字符串
  26. //使用方法`${变量名\表达式}`
  27. console.log(`${(teacher.lesson = "数学") ? "是数学老师" : "不是数学老师"}`);
  28. //模板函数,使用模板字面量为参数的函数
  29. //第一个参数为模板字面量中的字符串字面量组成的数组
  30. //第二个参数为模板字面量中的插值组成的数组
  31. function who(text, ...content) {
  32. console.log(`${text[0]}${content[0]}${text[1]}${content[1]}${text[2]}`);
  33. }
  34. who`这位老师是${class1}的${teacher.lesson}老师`;

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