Blogger Information
Blog 13
fans 0
comment 0
visits 10411
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实列演示模板字面量,标签函数以及解构赋值
微光
Original
722 people have browsed it

1、模板字面量

模板字面量用一对 ( `` ) 表含。变量用 $(变量名) 表示。
1、平时声明一个字符串,如果换行,需要用“\n\”字符:

  1. let qwe = "今天Chrome更新了87最新版,\n\
  2. 这是今年最后一次Chrome更新了";
  3. console.log(result);

用模板字面量就可以精简为:

  1. let qwe = `今天Chrome更新了87最新版,\n
  2. 这是今年最后一次Chrome更新了`;
  3. console.log(result);

2、 如果变量是拼接而成,例如:

  1. let a = 80;
  2. b = 90;
  3. c = 100;
  4. result =
  5. "语文:" + a + "数学:" + b + "英语:" + c + "总分:" + [a + b + c];
  6. console.log(result);

用模板字面量就可以精简为:

  1. let a = 80;
  2. b = 90;
  3. c = 100;
  4. result = `语文:${a} 数学:${b} 英语:${c} 总分:${ a + b + c} 分`;
  5. console.log(result);

2、标签函数

标签函数用 function show(固定文字组成的数组,变量,变量)表示。同时用show引用函数。
例如:

  1. let a = 80;
  2. b = 90;
  3. c = 100;
  4. function show(wer, s1, s2, s3) {
  5. let zbc = `${wer[0]}${a} ${wer[1]}${b} ${wer[2]}${c} ${wer[3]}${a + b + c}`;
  6. return zbc;
  7. }
  8. result = show`语文:${a} 数学:${b} 英语:${c} 总分:${a + b + c}`;
  9. console.log(result);

3、对象解构

1、声明的变量与对象的属性数量一样,进行完全解构。

  1. let user = {
  2. username: "admin",
  3. passwd: 123456,
  4. };
  5. let username, passwd;
  6. ({ username, passwd } = user);
  7. console.log(name, age);

2、声明的变量与对象的属性数量不一样,进行不完全解构。同时可以对未定义的变量进行内部赋值。

  1. let user = {
  2. username: "admin",
  3. passwd: 123456,
  4. };
  5. let username, passwd;
  6. ({ username, passwd,tel = 153 } = user);
  7. console.log(name, age,tel);

4、数组解构

1、两个数组内部变量一样时

  1. let name = [1, 2, 3, 4];
  2. let [a, b, c, d] = name;
  3. console.log(a, b, c, d);

如果数组中存在有数组时

  1. let name = [1, 2, [3], 4];
  2. let [a, b, [c], d] = name;
  3. console.log(a, b, c, d);

2、两个数组内部变量不一样时

  1. let name = [1, 2, 3, 4];
  2. let [a, b, ...d] = name;
  3. console.log(a, b, d);
Correcting teacher:天蓬老师天蓬老师

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