Blogger Information
Blog 37
fans 0
comment 0
visits 34817
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
解构与流程控制
手机用户1607314868
Original
560 people have browsed it

模板字符串

模板字面量,也叫模板字符串
模板字面量,可以理解为字符串的升级版,它完全可以代替传统的字符串声明,并且还可以在内部直接解析变量

  1. let site='www.baidu.com';
  2. site=`${site}(php中文网)`;
  3. console.log(site);
标签函数
  1. let num=20;
  2. let price=100;
  3. function fun(string,var1,var2){
  4. let res=`${string[0]}: ${var1}, ${string[1]}:${var2},${string[2]}:${var1 * var2}`;
  5. return res;
  6. }
  7. let sum=fun`商品数量 ${num}, 单价 ${price}, 总计 ${num * price}`;
  8. console.log(sum);
  9. string 代表 所有字符集合['商品数量','单价','总计'];

解构

任何类型都可以解构,用的最多的往往是对象和数组
注意:解构要求等号两边类型一致
1.对象解构

  1. <script>
  2. ({name,age}={name:'jacke',age:53});
  3. console.log(name,age);
  4. /*默认值*/
  5. ({name,age,email='qq@.com'}={name:'jacke',age:53});
  6. console.log(name,age,sex);
  7. /*别名*/
  8. ({name:ming,age:old}={name:'jacke',age:53});
  9. console.log(ming,old);
  10. </script>
  1. 数组解构
    数组解构跟对象解构大致一样,除了形式不一样
    1. <script>
    2. const colors=['red','green','blue'];
    3. let [color1,color2,color3]=colors;
    4. console.log(color1,color2,color3);
    5. </script>

    流程控制

    流程控制:分支
    1.if语句
  1. <script>
  2. let score=2;
  3. if(score>6)console.log('合格');
  4. else if(score>6 && score<8)console.log('优良');
  5. else console.log('不合格');
  6. </script>

2.switch

  1. <script>
  2. let res='success';
  3. switch(res){
  4. case 'fail':console.log('失败!');break;
  5. case 'success':console.log('成功!');break;
  6. default:console.log('平手');
  7. }
  8. </script>

注意:switch 通常不去做区间判断,而是用于单值判断
3.三元运算
let str=(score>=60)?'及格了':'补考吧!';

流程控制:循环
  • while循环
  1. <script>
  2. const arr=[1,2,3,4,5];
  3. let i=0;
  4. while(i<arr.length){
  5. console.log(arr[i]);
  6. i++;
  7. }
  8. i=0;
  9. do{
  10. console.log(arr[i]);
  11. i++;
  12. }while(i<arr.length);
  13. </script>
  • for循环
  1. <script>
  2. let arr=[1,2,3,4,5];
  3. for(let i=0;i<arr.length;i++){
  4. //当i大于等于2时,则退出,不再往下执行
  5. if(i>=2)break;
  6. console.log(arr[i]);
  7. }
  8. //对象遍历
  9. const user={
  10. id:5,
  11. name:'jacke',
  12. age:53
  13. };
  14. for(let key in user){
  15. //key是键
  16. console.log(`${key} --- ${user[key]}`);
  17. }
  18. </script>
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