Blogger Information
Blog 29
fans 0
comment 0
visits 18498
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
标签函数、解构与流程控制
CC
Original
633 people have browsed it

标签函数

  • 转移字符
  1. // // 模板字符串
  2. let name = 'chw';
  3. name = "nan";
  4. // 方案一
  5. name = "bao\" bao \"";
  6. // 方案二
  7. name = 'bao"bao"';
  8. // 方案三
  9. name= `bao'bao'`;
  10. console.log(name)
  • 字符串拼接
  1. // // 模板字符串
  2. let name = 'chw';
  3. let sex = "nan";
  4. console.log(name+sex)
  • 模板字符串与标签函数
    1. // // 模板字符串
    2. let name = 'chw';
    3. let sex = "nan";
    4. console.log(`${name}sex`)

    模板字符串实例(字符串引号不要,连接符+不要,变量使用${变量})

    1. let num = 30;
    2. let price = 100;
    3. // 字符串拼接的一般方式
    4. // let res ='商品数量'+num+',单价:'+price+'元,总计:'+num*price;
    5. // 模板字面量,使用插值进行简写,字符串引号不要,连接符+不要,变量使用${变量}
    6. let res =`商品数量${num},单价:${price}元,总计:${num*price}`;
    7. console.log(res)

解构

  • 标签函数(得到数组,用数组下标取值)
  1. let num = 10;
  2. let price = 99;
  3. // 字符串拼接与计算一般方式
  4. // let res ='商品数量'+num+',单价:'+price+'元,总计:'+num*price;
  5. // 模板字面量,使用插值进行简写,字符串引号不要,连接符+不要,变量使用${变量}
  6. let res =`商品数量${num},单价:${price}元,总计:${num*price}`;
  7. console.log(res)
  8. // strings代表字符串,var1表示变量1,var2表示变量2
  9. function show(strings,var1,var2){
  10. console.log(strings);
  11. console.log(var1,var2);
  12. }
  13. show`商品数量${num},单价:${price}元,总计:${num*price}`;

对象解构

  1. const teacher = {
  2. name:"zhu",
  3. age:18
  4. };
  5. let name =teacher.name;
  6. let age =teacher.age;
  7. console.log(name,age)
  1. const teacher = {
  2. name:"zhu",
  3. age:18
  4. };
  5. ({name,age}= {name:"zhu",age:18});
  6. console.log(name,age)

数组解构

  1. const teacher = ['name','age','sex']
  2. let [tc1,tc2,tc3]= teacher
  3. console.log(tc1,tc2,tc3)

流程控制

  • 分支

    1.单分支
    1.1一般解构

    1. let score = 70;
    2. if(score>=60){
    3. console.log('及格了');
    4. }

    1.2简写(大括号不写)

    1. let score = 70;
    2. if(score>=60) console.log('及格了');

    2.双分支

    1. let score = 50;
    2. if(score>=60) console.log('及格了');
    3. else console.log('补考吧,兄弟');

    2.1三元运算简化双分支

    1. let score = 80;
    2. let str = (score>=60)?'及格':'补考';
    3. console.log(score)

    3.多分支

    1. // &&且,||或
    2. let score = 120;
    3. if(score>=60&&score<80) console.log('及格了');
    4. else if (score>=81&&score<=100) console.log('优秀');
    5. // 成绩的有效区间
    6. else if (score>=0||score<=100) console.log('不存在');
    7. // 默认分支else
    8. else console.log('补考吧');

    4.switch

    1. let score = 120;
    2. // 区间正确的
    3. switch (true) {
    4. // 条件一
    5. case (score >= 60 && score < 80): console.log('及格了'); break;
    6. // 条件2
    7. case (score >= 81 && score <= 100): console.log('优秀'); break;
    8. // 条件3
    9. case (score >= 0 || score <= 100): console.log('不存在'); break;
    10. // 输出
    11. default: console.log('补考吧');
    12. }
  • while循环
    1. const arr =[1,2,3,4,5];
    2. console.log(arr);
    3. let i =0
    4. while(i<arr.length){
    5. console.log(arr[i]);
    6. i++
    7. }
  • for 循环

    数组遍历
    1.for (初始化变量;设置循环条件;更新循环条件) {**}

    1. const arr = [1,2,3,4,5]
    2. for (let i =0;i<arr.length;i++) console.log(arr[i])

    2.for与if

    1. const arr = [1,2,3,4,5]
    2. for (let i =0;i<arr.length;i++) {
    3. if(i<3)console.log(arr[i])
    4. }

    3对象遍历

    1. const tc={
    2. id:5,
    3. name:'zhu',
    4. qq:"8798",
    5. };
    6. for(let key in tc){
    7. console.log(tc[key])
    8. }

    4.数组遍历
    for-in遍历同Python

    1. arr =[123,"zhen",22]
    2. for(let x in arr){
    3. console.log(arr[x])
    4. }

5.foreach遍历数组是js常用

  1. let age= [10,10,30,123];
  2. // age 里面传方法foreach
  3. age.forEach(function(value){
  4. console.log(value)
  5. })
  6. // 箭头函数去掉function变成=>与value的小括号
  7. age.forEach(value=>{
  8. console.log(value)
  9. })
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