Blogger Information
Blog 64
fans 2
comment 1
visits 47050
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS对象与流程控制的理解
Y的博客
Original
611 people have browsed it

1. 实例演示值传递与引用传递的区别与联系

  1. <script>
  2. // 1. 赋值
  3. // 1.1 值传递: 原始类型,string,number,bool
  4. let a = 1;
  5. let b = a;
  6. console.log("a=%d,b=%d", a, b);
  7. a = 2;
  8. // 更新a,不影响b
  9. console.log("a=%d,b=%d", a, b);
  10. // 1.2 引用传递: 引用类型, object,array
  11. let obj1 = { a: 1, b: 2 };
  12. console.log(obj1);
  13. let obj2 = obj1;
  14. console.log(obj2);
  15. // 更新obj1
  16. obj1.a = 10;
  17. console.log(obj1);
  18. // obj2同步更新
  19. console.log(obj2);
  20. // 2. 传参
  21. // 传参时,无论什么类型都是值传递
  22. const f1 = (x) => (x = 10);
  23. let m = 5;
  24. console.log("m=%d", m);
  25. f1(m);
  26. // 入参:调用函数时传入的参数,简称:“入参”
  27. // 函数中对参数的更新,并不影响入参
  28. console.log("m=%d", m);
  29. const f2 = (x) => (x.a = 13);
  30. let o = { a: 1, b: 2 };
  31. console.log(o);
  32. f2(o);
  33. // 看上去函数中对于o.a的更新生效,实际上仍是值传递
  34. console.log(o);
  35. // 对与引用类型,只有全新赋值才算是更新,修改属性不算的
  36. const obj = { x: 1, y: 2 };
  37. obj.x = 20;
  38. const f3 = (x) => (x = {});
  39. f3(o);
  40. // 函数中对于对象参数/引用参数的更新并没有影响到入参
  41. console.log(o);
  42. </script>

2. 数组和对象解构的常用方法与函数传参

  1. <script>
  2. // 解构赋值:快速从集合数据(数组/对象)结构处独立变量
  3. // 1.数组
  4. let [a, b, c] = [1, 2, 3]
  5. console.log(a, b, c);
  6. // 解构一部分
  7. [a, b] = [1, 2, 3]
  8. console.log(a, b);
  9. [a, b, c = "null"] = [1, 2]
  10. console.log(a, b, c);
  11. [a, b, ...c] = [1, 2, 3, 4, 5, 6]
  12. console.log(a, b, c);
  13. // 拿到某一个值
  14. [, b, ] = [1, 2, 3]
  15. console.log(b);
  16. let x = 1,
  17. y = 2,
  18. t;
  19. console.log("x=%d,y=%d", x, y);
  20. // t = x;
  21. // x = y;
  22. // y = t;
  23. // console.log("x=%d,y=%d", x, y);
  24. // 用解构函数
  25. [x, y] = [y, x]
  26. console.log("x=%d,y=%d", x, y);
  27. // 2.对象解构
  28. let {
  29. id,
  30. name
  31. } = {
  32. id: 10,
  33. name: "bod"
  34. };
  35. console.log(id, name);
  36. let email = "admin@php.cn";
  37. let {
  38. role,
  39. email: userEmail
  40. } = {
  41. role: "user",
  42. email: "user@php.cn"
  43. };
  44. console.log(userEmail);
  45. console.log(email);
  46. // 3.参数解构
  47. // 数组传参
  48. let sum = ([a, b]) => a + b;
  49. console.log(sum([11, 22]));
  50. // 对象传参
  51. let getUser = ({
  52. name,
  53. sex
  54. }) => [name, sex];
  55. console.log(getUser({
  56. name: '小星星',
  57. sex: '男'
  58. }));
  59. </script>

3. call,apply,bind的区别与联系

  1. <script>
  2. function hello(name) {
  3. this.name = name;
  4. console.log(this.name);
  5. }
  6. const obj = {
  7. name: "admin",
  8. };
  9. // 经典调用
  10. console.log(hello("朱老师"));
  11. // bind()不会立即执行,只会返回一个函数声明
  12. let f = hello.bind(obj, "灭绝老师");
  13. console.log(f());
  14. // call/apply立即执行
  15. f = hello.call(obj, "天蓬老师");
  16. console.log(f);
  17. // apply参数必须是数组
  18. f = hello.apply(obj, ["西门老师"]);
  19. console.log(f);
  20. // bind()应用案例:动态改变this
  21. document.querySelector("button").addEventListener(
  22. "click",
  23. function() {
  24. console.log(this.name);
  25. console.log(this);
  26. document.body.appendChild(
  27. document.createElement('p')).innerHTML = "欢迎:" + this.name;
  28. }.bind({
  29. name: "peter老师"
  30. })
  31. );
  32. </script>

4. 访问器属性的原理与实现过程

  1. <script>
  2. const product = {
  3. data: [{
  4. name: "电脑",
  5. price: 5000,
  6. num: 5
  7. }, {
  8. name: "手机",
  9. price: 4000,
  10. num: 10
  11. }, {
  12. name: "相机",
  13. price: 8000,
  14. num: 3
  15. }, ],
  16. getAmounts() {
  17. return this.data.reduce((t, c) => (t += c.price * c.num), 0)
  18. },
  19. // 访问器属性
  20. // 将方法伪造成一个属性
  21. get total() {
  22. return this.data.reduce((t, c) => (t += c.price * c.num), 0)
  23. },
  24. set setPrice(price) {
  25. this.data[1].price = price;
  26. },
  27. }
  28. // console.log("总金额 :", product.getAmounts())
  29. console.log("总金额 :", product.total);
  30. console.log(product.data[1].price);
  31. product.setPrice = 9988;
  32. console.log(product.data[1].price);
  33. </script>

5. 多分支与swithc转换的技巧

  1. <script>
  2. let sorce = 10;
  3. // 单分支
  4. // if (sorce >= 60) {
  5. // console.log("合格");
  6. // }
  7. // 双分支
  8. // if (sorce >= 60) {
  9. // console.log("合格");
  10. // } else {
  11. // console.log("补考");
  12. // }
  13. // 多分支
  14. sorce = 120;
  15. if (sorce >= 60 && sorce < 80) {
  16. console.log("合格");
  17. } else if (sorce >= 80 && sorce < 100) {
  18. console.log("优秀");
  19. }
  20. // 判断数据是否有效
  21. else if (sorce > 100 || sorce < 0) {
  22. console.log("数据无效");
  23. } else {
  24. console.log("补考");
  25. }
  26. // switch来简化多分支
  27. // switch是严格匹配
  28. sorce = 90;
  29. switch (true) {
  30. case sorce >= 60 && sorce < 80:
  31. console.log("合格");
  32. break;
  33. case sorce >= 80 && sorce < 100:
  34. console.log("优秀");
  35. break;
  36. // 判断数据是否有效
  37. case sorce > 100 || sorce < 0:
  38. console.log("数据无效");
  39. break;
  40. default:
  41. console.log("补考");
  42. }
  43. // switch一般用来单值判断
  44. let response = "Success";
  45. // toLocaleLowerCase()全部转为小写
  46. switch (response.toLocaleLowerCase()) {
  47. case "fail":
  48. console.log("请求失败");
  49. break;
  50. case "success":
  51. console.log("请求成功");
  52. break;
  53. default:
  54. console.log("请求失败");
  55. }
  56. </script>

6. 三元运算符

  1. // 三元运算符
  2. // 条件? true : false
  3. if (sorce >= 60) {
  4. console.log("合格");
  5. } else {
  6. console.log("补考");
  7. }
  8. sorce = 6;
  9. // 用三元运算符简写
  10. // 用来写双分支
  11. console.log(sorce >= 60 ? "合格" : "补考");
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