博客列表 >PartIII 03 模板字面量与标签模板(0902wed)

PartIII 03 模板字面量与标签模板(0902wed)

老黑
老黑原创
2020年09月05日 21:58:59850浏览

主要内容:

  1. 数组结构中的不定元素
  2. 函数中的解构参数
  3. 解构声明应用场景
  4. 传统多行字符串与变量的嵌入(换行及空格的解决)
  5. 模板字面量/模板字符串
  6. 标签模板/模板标签
  7. 模板原始值

1. 数组结构中的不定元素

  • …rest, .concat
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>数组解构中不定元素</title>
  7. </head>
  8. <body>
  9. <script>
  10. let fruits = ["apple", "cherry", "peach", "pear", "Lemon", "mango"];
  11. // 任务:从第3个元素开始,将所有元素放到另一个数组中
  12. let arr = fruits.slice(2);
  13. console.log(arr);
  14. // ...name: ...rest:归并, ...sprad: 打散
  15. let [firstFruit, secondFruit, ...restFruits] = fruits;
  16. console.log(firstFruit, secondFruit);
  17. console.log(restFruits);
  18. console.log(...restFruits);
  19. // 数组合并
  20. let old = [1, 2, 3];
  21. let tmp1 = [8, 9, 10];
  22. let tmp2 = ["a", "b", "c"];
  23. let res = old.concat(tmp1, tmp2);
  24. console.log(res);
  25. console.log(old);
  26. console.log(old.concat(old, old));
  27. // 数组克隆
  28. let n = old.concat();
  29. console.log(n);
  30. // ...rest
  31. let [...newArr] = old;
  32. console.log(newArr);
  33. console.log(newArr === old);
  34. newArr[0] = 88;
  35. console.log(newArr);
  36. console.log(old);
  37. </script>
  38. </body>
  39. </html>

2. 函数中的解构参数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>函数中的解构参数</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 1. 传统方式
  11. let setUser = function (id, userInfo) {
  12. // 要求第二个参数必须是对象
  13. userInfo = userInfo || {};
  14. let name = userInfo.name;
  15. let email = userInfo.email;
  16. let status = userInfo.status;
  17. return { id, name, email, status };
  18. };
  19. let user = setUser(1);
  20. user = new setUser(1, {
  21. name: "admin",
  22. email: "admin@php.cn",
  23. status: true,
  24. });
  25. console.dir(user);
  26. // 2. 解构参数进行简化
  27. setUser = function (id, { name, email, status }) {
  28. return { id, name, email, status };
  29. };
  30. user = setUser(1, {
  31. name: "admin",
  32. email: "admin@php.cn",
  33. status: true,
  34. });
  35. console.dir(user);
  36. // user = setUser(3);
  37. // console.log(user);
  38. // 在解构中,禁止使用undefind,null来初始化
  39. // let { x, y } = undefined;
  40. setUser = function (
  41. id,
  42. { name = "defaultName", email = "defaultEmal", status = false } = {}
  43. ) {
  44. return { id, name, email, status };
  45. };
  46. user = setUser(3);
  47. console.log(user);
  48. const userInfo = {
  49. name: "username",
  50. email: "my@qq.com",
  51. status: true,
  52. };
  53. setUser = function (id, { name, email, status } = userInfo) {
  54. return { id, name, email, status };
  55. };
  56. setUser1 = function (id, { name, email, status } = userInfo) {
  57. return { id, name, email, status };
  58. };
  59. user = setUser(3);
  60. console.log(user);
  61. </script>
  62. </body>
  63. </html>

3. 解构声明应用场景

  • 变量交换
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>解构声明应用场景</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 变量交换
  11. let x = 10,
  12. y = 20,
  13. tmp;
  14. console.log("x = ", x, ", y = ", y);
  15. // tmp = x;
  16. // x = y;
  17. // y = tmp;
  18. // console.log("x = ", x, ", y = ", y);
  19. console.log("-----------------------------");
  20. // [x, y] = [x, y];
  21. [y, x] = [x, y];
  22. // console.log("x = ", x, ", y = ", y);
  23. console.log("x = %d, y = %d", x, y);
  24. </script>
  25. </body>
  26. </html>

4. 传统多行字符串与变量的嵌入

  • 会出现换行或空格问题
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>传统多行字符串与变量的嵌入</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 多行字符串
  11. let info =
  12. "This is a first line string \
  13. This is a second line string \
  14. This is three line string. ";
  15. info =
  16. "This is a first line string \n \
  17. \n \
  18. This is three line string. ";
  19. info = [
  20. "This is a first line string",
  21. "This is a second line string",
  22. "This is three line string",
  23. ].join("<br>");
  24. const p = document.createElement("p");
  25. p.innerHTML = info;
  26. document.body.appendChild(p);
  27. console.log(info);
  28. // 变量嵌入
  29. let list = ["汽车", "电脑", "水果"];
  30. let str = "";
  31. list.forEach(function (item) {
  32. str += "<li>" + item + "</li>";
  33. });
  34. const ul = document.createElement("ul");
  35. p.innerHTML = str;
  36. document.body.appendChild(ul);
  37. </script>
  38. </body>
  39. </html>

5. 模板字面量/模板字符串

  • 包括变量嵌入
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>模板字面量/模板字符串</title>
  7. </head>
  8. <body>
  9. <script>
  10. // es6 使用一对反引来解决前面的二大问题
  11. // 1. 多行字符串
  12. let str = `
  13. This is a first line string
  14. This is a second line string
  15. This is three line string.`;
  16. str = `
  17. <ul>
  18. <li>Peter</li>
  19. <li>peter@php.cn</li>
  20. <li>lecture</li>
  21. </ul>`.trim();
  22. console.log(str);
  23. // 2. 变量嵌入
  24. // 占位符: ${js表达式}
  25. let username = "Peter Zhu";
  26. // let message = "Hello " + username;
  27. let message = `Hello ${username} , 晚上好呀`;
  28. console.log(message);
  29. // 占位表达式支持计算
  30. console.log(`30 * 40 = ${30 * 40}`);
  31. function getUsername(username) {
  32. return username;
  33. }
  34. // 占位表达式支持函数
  35. console.log(`我的姓名是: ${getUsername("朱老师")}`);
  36. // 模板字面量支持嵌套
  37. // `${`模板字面量`}`
  38. console.log(`Hello ${`我的姓名是: ${getUsername("朱老师")}`}`);
  39. </script>
  40. </body>
  41. </html>

6. 标签模板/模板标签

  • 包括标签函数
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>标签模板/模板标签</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 1. 传统函数
  11. // alert("Hello php中文网");
  12. // 在一个模板字面量前面添加一个标签,就可以起到函数调用的效果
  13. // alert`Hello php中文网`;
  14. function getUser(name, email) {
  15. console.log("My name is ", name);
  16. console.log("My email is ", email);
  17. }
  18. let name = "admin";
  19. let email = "admin@php.cn";
  20. // getUser(name, email);
  21. // 用标签模板来调用它
  22. // getUser`${name}, ${email}`;
  23. // 标签模板: 模板字面量前是一个"标识符,本质上是一个函数"
  24. // 所以,我们可以认为标签模板是函数调用的特殊形式
  25. // 函数名: 模板字面量前面的标识符
  26. // 调用参数: 标签后面的模板字面量
  27. // 2. 标签函数
  28. // tag(strings, ...values)
  29. let width = 100;
  30. let height = 30;
  31. // 标签后面的模板字面量必须要保证第一个和最后一个必须是字符串
  32. let area = calculateArea`Width: ${width} * Height: ${height} = Area: ${
  33. width * height
  34. }`;
  35. // 定义这个标签对应的函数
  36. function calculateArea(strings, ...values) {
  37. // console.log(strings);
  38. // console.log(values);
  39. // 当前模板字面量中的字面量数组元素数量总是比表达式占位符数量多1
  40. // console.log(strings.length === values.length + 1);
  41. let result = "";
  42. for (let i = 0; i < values.length; i++) {
  43. result += strings[i];
  44. result += values[i];
  45. }
  46. // 添加最后一个字符字面量到结果中
  47. result += strings[strings.length - 1];
  48. return result;
  49. }
  50. // console.log(area);
  51. // 3. 模板原始值
  52. let msg = `Hello \n php.cn`;
  53. console.log(msg);
  54. let str = String.raw`Hello \n php.cn`;
  55. console.log(str);
  56. function getRaw(strings, ...values) {}
  57. </script>
  58. </body>
  59. </html>

7. 模板原始值

  • 最后一个貌似空字符串,但却很有用。后面vue等里面会用到
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>模板原始值</title>
  7. </head>
  8. <body>
  9. <script>
  10. let msg = `Hello \n php.cn`;
  11. console.log(msg);
  12. let str = String.raw`Hello \n php.cn`;
  13. console.log(str);
  14. // 标签函数
  15. function getRaw(strings, ...values) {
  16. console.log(strings);
  17. let result = "";
  18. for (let i = 0; i < values.length; i++) {
  19. result += strings.raw[i];
  20. result += values[i];
  21. }
  22. // 添加最后一个字符字面量到结果中
  23. result += strings.raw[strings.length - 1];
  24. return result;
  25. }
  26. let site = "php中文网";
  27. msg = getRaw`Hello \n ${site}`;
  28. console.log(msg);
  29. </script>
  30. </body>
  31. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议