Blogger Information
Blog 41
fans 0
comment 0
visits 31058
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组与对象的解构及常用的流程控制
陈强
Original
592 people have browsed it

模板字面量

  • 模板字面量, 也叫模板字符串
  1. let name = 'jack';
  • 模板字面量可以进行插值
  1. let num = 30;
  2. let price = 100;
  3. let res = '商品数量:' + num + ' , 单价: ' + price + ' 元, 总计: ' + num * price;
  4. //插值简写用``包含,表达式写在${express}中
  5. res= `商品数量:${num},单价:${price},总计:${num*price}`;

标签函数

  1. function show(strings, var1, var2) {
  2. console.log(strings);
  3. console.log(var1, var2);
  4. let res = `<ul>商品信息:<li>${strings[0]}: ${var1} </li>
  5. <li>${strings[1]}: ${var2}</li>
  6. <li>${strings[2]}: ${var1 * var2}</li></ul>`;
  7. console.log(res);
  8. return res;
  9. }
  10. coust ul = show`商品数量${num}单价${price}总计${num*price}`;

解构赋值

对象解构

  1. coust MAN = {
  2. name = 'jack',
  3. age = 18
  4. }
  5. //解构要求等号二边类型一致
  6. ({name,age} = {name:"jack",age:18} );
  7. console.log(name,age);
  8. //可以插入元素并设置默认值
  9. ({name,age,gender="man"} = {name:"jack",age:18});
  10. console.log(name.age.gender);
  11. //元素支持别名
  12. ({name:userName,age:useAge} = {name:"jack",age:18});
  13. console.log(userName,userAge);

数组解构

  1. //声明数组
  2. coust arr = [1,2,3,4];
  3. //等号左侧看做模板
  4. let [num1.num2,num3,num4] = arr;
  5. console.log(num1,num2,num3.num4);
  6. let [a, [b, c], d] = [10, [20, 30], 40];
  7. console.log(a, b, c, d);
  8. //设置初始值
  9. let [x, y, z = 999] = [100, 200];
  10. console.log(x, y, z);

流程控制

分支

  • 单分支
  1. let a = 70
  2. if (a > 60) console.log('大于60');
  • 双分支
  1. let a = 50;
  2. if (a > 60) console.log('大于60');
  3. else console.log('小于60');
  • 多分支
  1. let a = 90;
  2. if (a >= 60 && a <= 80) console.log('在60~80之间');
  3. elseif (a >= 81 && a <= 90 ) console.log('在80~90之间');
  4. else console.log('不在60~90之间');
  • switch 多分支
  1. a = 50;
  2. switch (true) {
  3. case a >= 60 && a <= 80:
  4. console.log('在60~80之间');break;
  5. case a >= 81 && a <= 90:
  6. console.log('在80~90之间');break;
  7. default console.log('不在60~90之间);
  8. }
  9. //switch通常不去做区间判断,而是用于单值判断
  10. let response = 'Success';
  11. // 对于单值的字符器需要将格式统一化,例如全转为大写或小写
  12. switch (response.toLowerCase()) {
  13. case 'fail':
  14. console.log('请求失败'); break;
  15. case 'success':
  16. console.log('请求成功'); break;
  17. case 'error':
  18. console.log('参数错误'); break;
  19. default: console.log('未知错误');
  20. }
  • 三元运算符
  1. a = 50;
  2. console.log(a > 60 ? '大于60' : '小于60');

循环

  • while 入口判断型
  1. let a = 0;
  2. while (a <= 5) {
  3. console.log(a);
  4. a++;
  5. }
  • do while 出口判断型
  1. let a = 0;
  2. do {
  3. console.log(a);
  4. a++;
  5. }while(a <= 5);
  6. //出口判断型即使不满足当前条件也会默认执行一次,例如 lat a = 6;
  7. //do while 也会执行一次,输出6;
  • for 循环
  1. for (a = 0; a <= 5; a++) {
  2. console.log(a);
  3. }
  • for循环和分支混合使用
  1. for (a = 0; a <= 5; a++) {
  2. if (a <= 3) console.log(a);
  3. }
  • 对象遍历

    • for in
      1. const man = {
      2. name: "jack",
      3. age: 18,
      4. };
      5. for (let key in man) {
      6. console.log(`${key}=>${man[key]}`);
      7. }
  • 数组遍历

    • for of
      ```html
      const arr = [1, 2, 3, 4];
      for (let value of arr) {
      console.log(value);
      }

```

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