Blogger Information
Blog 41
fans 0
comment 0
visits 41147
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS值传递|引用传递|数组和对象解构|函数传参|访问器属性原理实现|多分支与swithc转换|三元运实例演示
幸福敲门的博客
Original
802 people have browsed it
  1. 实例演示值传递与引用传递的区别与联系;
  2. 数组和对象解构的常用方法与函数传参
  3. call,apply,bind的区别与联系,并试着实例演示一下
  4. 访问器属性的原理与实现过程,并实例演示;
  5. 多分支与swithc转换的技巧
  6. 三元运算解决了什么问题,有什么限制?

一、值传递|引用传递

1.1 值传递: 原始类型,string,number,bool

  1. <script>
  2. let a = 10;
  3. let b = a;
  4. console.log("a = %d, b = %d", a, b);
  5. a = 15;
  6. // 更新a,不影响b
  7. console.log("a = %d, b = %d", a, b);
  8. </script>

图示:
值传递

1.2引用传递: object,array

  1. <script>
  2. let obj1 = { a: 5, b: 18 };
  3. console.log(obj1);
  4. let obj2 = obj1;
  5. console.log(obj2);
  6. // 更新obj1
  7. obj1.b =30;
  8. console.log(obj1);
  9. // obj2同步更新
  10. console.log(obj2);
  11. </script>

图示:
object应用传递

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

2.1数组解构

  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);

图示:
数组解构

2.2对象解构

  1. <script>
  2. let { id, age,name } = { id: 10, age:3143,name:"北京"};
  3. console.log(id,age, name);
  4. // 属性名与变量名必须一一对应,顺序无所谓
  5. ({ id,age,name } = { id: 10, age:3143,name:"北京"});
  6. console.log(id, age,name);
  7. let email = "admin@php.cn";
  8. let { role, email: userEmail } = { role: "user", email: "user@php.cn" };
  9. console.log(userEmail);
  10. console.log(email);
  11. </script>

图示:
对象解构

2.3函数传参
数组传参

  1. <script>
  2. let sum = ([a, b,c, d]) => a - b-c-d;
  3. console.log(sum([150, 27,29,14,33]));
  4. </script>

图示:
数组传参
对象传参

  1. <script>
  2. let getUser = ({ name, age, sex}) => [name, age, sex];
  3. console.log(getUser({name:"黄蓉", age:18,sex:"girl"}));
  4. </script>

图示:
对象传参

三、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. console.log(hello("朱老师"));
  10. let f = hello.bind(obj, "天蓬老师");
  11. console.log(f());
  12. f = hello.call(obj, "灭绝老师");
  13. console.log(f);
  14. f = hello.apply(obj, ["西门老师"]);
  15. console.log(f);
  16. document.querySelector("button").addEventListener(
  17. "click",
  18. function () {
  19. console.log(this.name);
  20. console.log(this);
  21. }.bind({ name: "猫科动物" })
  22. );
  23. </script>

图示:
bind()不会立即执行

call/apply立即执行
bind()不会立即执行,只返回一个函数声明
bind()应用案例: 动态改变this

四、访问器属性的原理与实现过程

实例:

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

图示:
访问器属性

总结:访问器:将方法伪造成一个属性,访问器属性优先级高于同名的普通属性

五、多分支与swithc转换

多分支

  1. <script>
  2. let score = 120;
  3. if (score >= 60 && score < 80) {
  4. console.log("合格");
  5. } else if (score >= 80 && score <= 100) {
  6. console.log("学霸");
  7. }
  8. // 判断成绩是否合法
  9. else if (score > 100 || score < 0) {
  10. console.log("非法数据");
  11. } else {
  12. console.log("补考");
  13. }
  14. </script>

图示:
多分支

多分支与swithc转换

  1. <script>
  2. let = 120;
  3. switch (true) {
  4. case score >= 60 && score < 80:
  5. console.log("合格");
  6. break;
  7. case score >= 80 && score <= 100:
  8. console.log("学霸");
  9. break;
  10. // 判断成绩是否合法
  11. case score > 100 || score < 0:
  12. console.log("非法数据");
  13. break;
  14. default:
  15. console.log("补考");
  16. }

多分支与swithc转换

switch来简化多分支
switch是严格匹配

六、三元运算

  1. <script>
  2. let score = 90;
  3. console.log(score >= 60 ? "及格" : "补考");
  4. </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