Blogger Information
Blog 37
fans 2
comment 0
visits 26660
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1229-js数组与对象的解构-常用的流程控制
世纪天城
Original
469 people have browsed it

js解构赋值
任何类型都可以解构,但是工作中基本上只会用到对象和数组

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <!-- 任何类型都可以解构,但是工作中基本上只会用到对象和数组 -->
  11. <script>
  12. // 1. 对象解构
  13. // 原始结构
  14. const user = {
  15. name: 'js老师',
  16. age: 18,
  17. };
  18. // 解构要求等号二边类型一致例如:
  19. // 注意: 对象解构时需要用()将其全部包住
  20. ({ name, age } = { name: 'js', age: 18 });
  21. console.log(name, age);
  22. // 对象解构支持默认值如下
  23. ({ name, age, sex = '男'} = { name: 'js', age: 18 });
  24. console.log(name, age, sex);
  25. // 数组解构
  26. const colors = ['red', 'green', 'blue'];
  27. // 将左边看成模板
  28. // 将左边看成模板
  29. // 将colors数组中的值依次赋值到左边的变量中
  30. let [color1, color2, color3] = colors;
  31. console.log(color1, color2, color3);
  32. // 数组嵌套解构
  33. let [a, [b, c], d] = [1, [2, 3], 4];
  34. console.log(a, b, c, d);
  35. // 数组解构的默认值
  36. let [x, y, z = 999] = [100, 200];
  37. console.log(x, y, z);
  38. </script>
  39. </body>
  40. </html>

流程控制:分支

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>流程控制:分支</title>
  8. </head>
  9. <body>
  10. <script>
  11. // if单分支判断语句
  12. let score = 120;
  13. if(score>=60) console.log('及格');
  14. // if双分支判断语句
  15. if(score>=60) console.log('及格');
  16. else console.log('不及格');
  17. // if多分支判断语句
  18. // &&可以理解为 并且
  19. // ||为或,与 都可以
  20. if(score>= 60&& score<=80) console.log('及格');
  21. else if(score>80&&score<=100) console.log('学霸');
  22. else if(score>100||score<0) console.log('分数无效');
  23. else console.log('不及格');
  24. // switch语句 通常不去做区间判断,而是用于单值判断
  25. score =70
  26. switch(true){
  27. case score>=60&&score<=80:
  28. console.log('及格');break;
  29. case score>80&&score<=100:
  30. console.log('学霸');break;
  31. case score>100||score<0:
  32. console.log('分数无效');break
  33. default:console.log('不及格');
  34. }
  35. // switch对于单值的字符器需要将格式统一化,例如全转为大写或小写
  36. let response = 'Success';
  37. switch (response.toLowerCase()) {
  38. case 'fail':
  39. console.log('请求失败'); break;
  40. case 'success':
  41. // 当response中的值与success相同时执行
  42. console.log('请求成功'); break;
  43. case 'error':
  44. console.log('参数错误'); break;
  45. default: console.log('未知错误');
  46. }
  47. // 使用三元运算来简化"双分支"
  48. score = 30
  49. console.log(score >= 60 ? '及格了' : '补考吧,兄弟');
  50. // score >= 60为表达式
  51. // ?判断值为真时输出
  52. // :相当于else
  53. </script>
  54. </body>
  55. </html>

流程控制-循环

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>流程控制-循环</title>
  8. </head>
  9. <body>
  10. <script>
  11. // while循环
  12. // 定义数组
  13. const arr = [1,2,3,4,5,6];
  14. // let i =0;
  15. // while(i<arr.length){
  16. // console.log(arr[i]);
  17. // i++
  18. // }
  19. // for (初始化循环变量; 设置循环条件; 更新循环条件) { ...}
  20. for(let i=0;i<arr.length;i++){
  21. console.log(arr[i]);
  22. }
  23. // 对象的遍历for - in
  24. const user = {
  25. id: 5,
  26. name: 'php老师',
  27. email: 'php@php.cn'
  28. };
  29. for(let k in user){
  30. // console.log(user[k]);
  31. console.log(`${k}=>${user[k]}`);
  32. }
  33. // for - of 更加通用 它可以对象和数组的遍历
  34. for (let value of arr) {
  35. console.log(value);
  36. }
  37. </script>
  38. </body>
  39. </html>
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