Blogger Information
Blog 46
fans 0
comment 0
visits 39606
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演示: js(数组、对象)解构 与 常用流程控制
lus菜
Original
709 people have browsed it

数组结构演示:

  1. <script>
  2. // 数组结构
  3. let [a, [b, c], d] = [10, [20, 30], 40];
  4. //console.log数组输出
  5. console.log(a, b, c, d);
  6. </script>
效果预览:

对象的解构:

  1. <script>
  2. const user = {
  3. name: '姓名',
  4. age: '年龄',
  5. id: '性别',
  6. wcd: '学校',
  7. sdg: '地址',
  8. };
  9. ({name,age,id,wcd,sdg} = { name:'姓名',age:'年龄',id:'性别',wcd:'学校',sdg:'地址'})
  10. console.log(name,age,id,wcd,sdg);
  11. </script>
效果预览:

常用的流程控制方法:

  1. 两种控制方法:
  2. 分支:单分支、双分支、多分支
  3. 循环: while:入口判断型 出口判断型

单分支:

  1. <script>
  2. let score = 80;
  3. if (score >= 60) console.log("合格");
  4. </script>
效果预览:

双分支:

  1. <script>
  2. let score = 0;
  3. if (score >= 60) console.log("合格");
  4. else console.log("洗洗睡吧,兄弟");
  5. </script>
效果预览:

多分支

  1. <script>
  2. let score = 81;
  3. if(score >= 60 && score <= 80) console.log('合格');
  4. else if(score >= 81 && score <= 100) console.log('优秀');
  5. </script>
效果预览:

while:入口判断型:

  1. <script>
  2. //循环
  3. const arr = [1, 2, 3, 4, 5];
  4. //初始化循环变量
  5. let i = 0;
  6. //设置循环条件
  7. while (i < arr.length) {
  8. console.log(arr[i]);
  9. //更新循环条件
  10. i++
  11. }
  12. </script>
效果预览:

do - white:出口判断型:

  1. <script>
  2. const arr = [1, 2, 3, 4, 5];
  3. i = 0;
  4. // 设置循环事件
  5. do {
  6. console.log(arr[i]);
  7. // 更新循环条件
  8. i++
  9. } while (i < arr.length);
  10. </script>
效果预览:

for循环:

  1. <script>
  2. const arr = [1, 2, 3, 4, 5];
  3. i = 0;
  4. // for (初始化循环变量;设置循环条件; 更新循环条件) {...}
  5. for (let i = 0; i < arr.length; i++)console.log(arr[i]);
  6. </script>

对象的遍历:

  1. <script>
  2. const arr = [1, 2, 3, 4, 5];
  3. let i = 0;
  4. // for - in 对象遍历
  5. const user = {
  6. id: 666,
  7. name: '邮箱',
  8. email:'php@qq.cn',
  9. };
  10. for (let key in user) {
  11. console.log(`${key} => ${user[key]}`);
  12. }
  13. //for - of 数组遍历
  14. for (let value of arr) {
  15. console.log(value);
  16. }
  17. </script>

效果预览:

循环与分支经常混编:

  1. <script>
  2. const arr = [1, 2, 3, 4, 5];
  3. let i = 0;
  4. for (let i = 0; i < arr.length; i++) {
  5. //奇数与偶数
  6. if (arr[i] % 2 === 0) continue;
  7. console.log(arr[i]);
  8. }
  9. </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