Blogger Information
Blog 56
fans 0
comment 4
visits 37846
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS: 使用模板字面量作为参数的函数
异乡客
Original
537 people have browsed it

  1. // !!使用模板字面量作为参数的函数
  2. // 模板函数:第一个参数是字面量组成的数字,第二个参数起,是字面量中的插值列表
  3. console.log(`${0 < 20 ? `大于` : `小于`}`);
  4. // 第一步:申明模板函数
  5. function total(strings,num,price) {
  6. console.log(strings);
  7. console.log(num,price);
  8. let res = `${strings[0]}${num}${strings[1]}${price}${strings[2]}${num * price} ${strings[3]}`;
  9. console.log(res);
  10. return res;
  11. }
  12. let num = 10;
  13. let price =20;
  14. // 第二步:调用模板函数
  15. // total`数量${num}单价${price}的总金额:${num * price}元`;
  16. console.log(total`数量${num}单价${price}的总金额:${num * price}元`);
  17. // !!模板函数的优化,使用...rest将插值进行合并
  18. // 第一步...rest 将插值合并
  19. function sum(strings,...args){
  20. console.log(strings);
  21. console.log(args);
  22. console.log(`[${args.join()}]之和是:${args.reduce((a,c)=>a+c)}`);
  23. }
  24. // 第二部:调用
  25. sum`计算多个数之和:${1}${2}${3}${4}${5}${6}${7}`;
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