Blogger Information
Blog 18
fans 0
comment 0
visits 15994
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
值传递、数组对象的解构和多分支控制
沉寂的博客
Original
670 people have browsed it

值传递、数组对象的解构和多分支控制

  赋值分为值传递和引用传递,值传递用于原始类型,string number bool 值传递更新相同的变量不影响同类引用传递是用于引用类型 object array更新一个值以后相关联的也会改变;
相应实例如下所示:
值传递:

  1. let a = 1;
  2. let b = a;
  3. a = 2;
  4. // 更新a不影响b的值
  5. console.log("a=%d,b=%d", a, b);
  6. //引用传递
  7. let obj1 = { a: 12, b: 13, c: 14 };
  8. let obj2 = obj1;
  9. console.log(obj1, obj2);
  10. obj1.a = 15;
  11. console.log(obj1, obj2);

运行结果:
值传递

数组对象的解构

数组对象的解构主要是为了方便简化赋值,如下代码所示:

  1. let [a, b, c] = [1, 2, 3];
  2. console.log(a, b, c);
  3. [a, b] = [1, 2, 3];
  4. console.log(a, b);
  5. [a, b, c, d = "xxxx"] = [1, 2, 3];
  6. console.log(a, b, c, d);
  7. [a, b, ...c] = [1, 2, 3, 4, 5];
  8. console.log(a, b, c);
  9. [, , a, ,] = [1, 2, 3, 4, 5];
  10. console.log(a);
  11. let x = 1,
  12. y = 2,
  13. t;
  14. console.log("x = %d, y = %d", x, y);
  15. // t = x;
  16. // x = y;
  17. // y = t;
  18. // console.log("x = %d, y = %d", x, y);
  19. [y, x] = [x, y];
  20. console.log("x = %d, y = %d", x, y);
  21. // 2. 对象解构
  22. let { id, name } = { id: 10, name: "手机" };
  23. console.log(id, name);
  24. // 属性名与变量名必须一一对应,顺序无所谓
  25. ({ name, id } = { id: 10, name: "手机" });
  26. console.log(id, name);

多分支控制和单分支控制

  单分支控制用if(条件){代码} else{代码}
也可以用三元表达式,三元表达式的写法:判断条件?true成功的结果:fail失败的结果;
三元表达式只能用来判断单一条件的语句,多分支判断可以用if(条件){代码} else if(条件){代码}else{代码}来判断,多分支还可以用switch来简化,代码结构:
switch(true代码执行是否执行) {case 条件:代码段;break; case 条件:代码;break;default:代码;}
代码如下所示:

  1. if (score >= 60) {
  2. console.log("及格");
  3. // 默认分支
  4. } else {
  5. console.log("补考");
  6. }
  7. // 多分支
  8. score = 98;
  9. if (score >= 60 && score < 80) {
  10. console.log("合格");
  11. } else if (score >= 80 && score <= 100) {
  12. console.log("学霸");
  13. }
  14. // 判断成绩是否合法
  15. else if (score > 100 || score < 0) {
  16. console.log("非法数据");
  17. } else {
  18. console.log("补考");
  19. }
  20. // switch来简化多分支
  21. // switch是严格匹配
  22. score = 198;
  23. switch (true) {
  24. case score >= 60 && score < 80:
  25. console.log("合格");
  26. break;
  27. case score >= 80 && score <= 100:
  28. console.log("学霸");
  29. break;
  30. // 判断成绩是否合法
  31. case score > 100 || score < 0:
  32. console.log("非法数据");
  33. break;
  34. default:
  35. console.log("补考");
  36. }
  37. // switch用在单值判断
  38. let response = "Success";
  39. switch (response.toLowerCase()) {
  40. case "fail":
  41. console.log("请求失败");
  42. break;
  43. case "success":
  44. console.log("请求成功");
  45. break;
  46. default:
  47. console.log("未知错误");
  48. }
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