Blogger Information
Blog 18
fans 0
comment 0
visits 7737
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数与返回值,模板字面量与模板函数
只如初见
Original
368 people have browsed it

函数参数与返回值

  1. // 1. 参数不足: 默认参数
  2. f = (a, b = 0) => a + b;
  3. console.log(f(10, 5));
  4. // 2. 参数过多: ...剩余参数
  5. f = (...arr) => arr.reduce((a, c) => a + c);
  6. console.log(f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
  7. // 返回值: 函数只能有一个返回值,默认单值返回
  8. // 需要返回多个值怎么办? 数组,对象
  9. let fn = () => [1, 2, 3];
  10. let res = fn();
  11. //返回数组
  12. console.log(res);
  13. fn = () => ({
  14. id: 3,
  15. name: 'abc',
  16. age: 15,
  17. });
  18. res = fn();
  19. //返回对象
  20. console.log(res);

模板字面量与模板函数

  1. //1 模板字面量
  2. console.log('Hello world');
  3. // 反引号:模板字面量, 支持在字符串插入变量/表达式: 插值
  4. let name = '小刘';
  5. console.log('hello ' + name);
  6. // 变量/表达式: 在模板字面量,使用 '${xxx}'来引用,就是一个占位符
  7. console.log(`hello ${name}`);
  8. let gender = 1;
  9. console.log(`${gender ? `男:${name}` : `女`}`);
  10. // 2. 模板函数 (使用模板字面量为参数的参数)
  11. calc`数量: ${10}单价: ${500}`;
  12. function calc(strings, ...args) {
  13. console.log(strings);
  14. console.log(args);
  15. console.log(args[0] * args[1]);
  16. }
  17. /**
  18. * * 模板字面量: 可以使用插值表达式的字符串
  19. * * 模板函数: 可以使用"模板字面量"为参数的函数
  20. * * 模板函数,就是在"模板字面量"之前加一个标签/标识符,而这个标签,就是一个函数名
  21. * * 模板函数的参数是有约定的, 不能乱写, 第一个是字面量数组,从第二起才是内部的占位符参数
  22. */
  23. // * 模板字面量, 也叫"模板字符串" , 是同义词,我觉得用"模板字面量"更直观,准确
  24. // * 模板函数, 有的书也翻译与"标签函数", 因为 它使用"模板字面量"做参数,称为"模板函数"更直观, 一看知识必须传一个模板字面量当参数
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!