Blogger Information
Blog 12
fans 0
comment 0
visits 6519
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模板字面量、标签函数、解构赋值与对象字面量的简化
Giesen?
Original
698 people have browsed it

模板字面量与标签函数

  1. // 1 模板字面量
  2. let Hello = "hello";
  3. // 注意标识符是 ` `
  4. let html = `${Hello} html`;
  5. console.log(html);
  6. let username = "admin";
  7. // 模板字面量,es6中引入的
  8. let str = `Hello
  9. ${username}`;
  10. console.log(str);
  11. let nav = ["小明" , "小美" , "小新"] ;
  12. html =`
  13. <nav>
  14. <a href = "">${nav[0]}</a>
  15. <a href = "">${nav[1]}</a>
  16. <a href = "">${nav[2]}</a>
  17. </nav>
  18. `;
  19. console.log(html);
  20. // document.body.insertAdjacentHTML("beforeend", html);
  21. // 标签函数 自定义模板字面量的行为
  22. // 多值插入需要一个起始和终止的参数
  23. show = function (strs,...arr) {
  24. console.log(arr);
  25. //查看数组模板
  26. console.log(strs);
  27. //这里执行 加法运算
  28. console.log(arr[0] + arr[1] + arr[2] + arr[1]);
  29. }
  30. let a = 20;
  31. let b = 10 ;
  32. let c = 20 ;
  33. let arrr = 80 ;
  34. // 在函数中插入值 不需要等于号
  35. // 这里决定 数组 有多少 数值 这里的先后顺序决定 插入的值在数组中的位置
  36. show `${arrr} ${a} ${b} ${c} `;

解构赋值与对象字面量的简化

  1. // 解构赋值
  2. //将数组的每个元素,分别创建变量进行储存
  3. let arr = [1,2,3];
  4. let a = arr[0];
  5. let b = arr[1];
  6. let c = arr[2];
  7. console.log(a,b,c);
  8. // 用解构进行简化
  9. // let [A,B,C] = [4,5,6];
  10. // console.log(a,b,c);
  11. // ...d 值为 剩下没赋值的数组元素
  12. [a, b, c,e ,...d] = [1, 2, 3, 4, 5, 6];
  13. console.log(a, b, c,e);
  14. console.log(d);
  15. // 对象解构
  16. let {id,name} = {id:10 , name:"小名"};
  17. console.log(id,name);
  18. // 参数解构
  19. let sum = ([a, b]) => a + b;
  20. console.log(sum([10, 20]));
  21. // 使用归并参数
  22. sum = (...[a, b,c]) => a + b +c ;
  23. // 如果只想传独立变量, 可以将函数参数进行解包spread;
  24. console.log(sum(10, 20,10));
  25. console.log(...[30, 20, 30,40]);
  26. // 对象字面量简化
  27. let person = {
  28. name1: "z老师",
  29. email: "498668472@qq.com",
  30. class1: "706",
  31. getInfo: function () {
  32. // 在对象中使用 this 来访问自边的成员
  33. return `${this.name1} : ( ${this.email} )`;
  34. },
  35. };
  36. console.table(person.getInfo());
  37. // 用对象解构 简化
  38. // 打印出来 persom变量中的 字符串
  39. let { name1, email,class1 } = person;
  40. console.log( name1, email,class1)
  41. // let user = {
  42. // name1,
  43. // email,
  44. // getInfo: function () {
  45. // // 在对象中使用 this 来访问自边的成员
  46. // return `${this.name1} : ( ${this.email} )`;
  47. // },
  48. // };
  49. // console.log(user);
  50. // 箭头函数简化
  51. user = {
  52. name,
  53. email,
  54. // 将方法后面的冒号和'function'删除
  55. getInfo: () => `${this.name} : ( ${this.email} )`,
  56. };
  57. console.log(user);
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!