Blogger Information
Blog 40
fans 0
comment 1
visits 24386
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第15章 0105-JS对象与流程控制,学习心得、笔记
努力工作--周工--Robin
Original
529 people have browsed it

今天所学心得、笔记

1、值传递与引用传递

  1. // 值传递, 用aaa赋值给变量bbb,当aaa的值改变了,bbb值不会变
  2. let aaa = 100;
  3. let bbb= aaa; //值传递
  4. console.log("aaa = %d, bbb = %d", aaa, bbb);
  5. aaa = 200;
  6. console.log("aaa = %d, bbb = %d", aaa, bbb);
  7. //引用传递,改变的是变量的引用地址,地址里的值变了,
  8. //所有引用该地址的变量,值都会改变
  9. let obj_a = { a: 100, b: 200 };
  10. console.log(obj_a);
  11. let obj_b = obj_a;
  12. console.log(obj_b);
  13. // 更新obj_a
  14. obj_a.a = 300;
  15. console.log(obj_a);
  16. // obj_b同步更新
  17. console.log(obj_b);

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

数组解构

  1. let [a1, b1, c1] = [1, 2, 3];
  2. console.log(a1 , b1 , c1);
  3. let [a2, b2, c2, d2=9, e2] = [1, 2, 3];
  4. console.log(a2 , b2 ,c2, d2, e2);
  5. let [a3, b3, ...c3] = [1, 2, 3, 4, 5];
  6. console.log(a3 , b3 , c3);
  7. let [, , d, , ,] = [1, 2, 3, 4, 5];
  8. console.log(d);

对象解构

  1. let {id, name} = {id:10, name:"手机"};
  2. console.log(id, name);
  3. ({id, name} = {id:20, name:"笔记本"});
  4. console.log(id, name);
  5. //变量冲突,起别名
  6. let email = "admin@php.cn";
  7. let {role, email:uEmail} = {role:"user", email:"user@php.cn"};
  8. console.log(email);
  9. console.log(role, uEmail);

参数解构

  1. // 数组传参
  2. let sum = ([a, b]) => a +b;
  3. console.log(sum([10, 20]));
  4. // 对象传参
  5. let getUser = ({name, email}) => [name, email];
  6. console.log(getUser({name: "天蓬老师", email:"tp@php.cn"}));

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

  1. function hello(name) {
  2. this.name = name;
  3. console.log(this.name);
  4. }
  5. const obj = {
  6. name: "admin",
  7. };
  8. // 经典调用
  9. console.log(hello("朱老师"));
  10. // bind()不会立即执行,只返回一个函数声明
  11. let f = hello.bind(obj, "天蓬老师");
  12. console.log(f());
  13. // call/apply立即执行
  14. f = hello.call(obj, "灭绝老师");
  15. console.log(f);
  16. f = hello.apply(obj, ["西门老师"]);
  17. console.log(f);

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

  1. const product = {
  2. data: [
  3. { name: "电脑", price: 1000, num: 10 },
  4. { name: "手机", price: 2000, num: 20 },
  5. { name: "相机", price: 3000, num: 30 },
  6. ],
  7. get total() {
  8. return this.data.reduce((t, c)=>(t += c.price * c.num), 0);
  9. },
  10. set setPrice(price) {
  11. this.data[1].price = price;
  12. },
  13. };
  14. console.log(product.total);
  15. console.log(product.data[1].price);
  16. product.setPrice = 8899;
  17. console.log(product.data[1].price);

5、多分支与swithc

  1. // if多分支
  2. score = 80;
  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("非法数据,请输入0--100之间的数值");
  11. } else {
  12. console.log("补考");
  13. }
  14. // switch判断
  15. //switch是严格匹配, 在这个例程里()内一定要用true
  16. score = 85;
  17. switch (true) {
  18. case score >= 60 && score < 80:
  19. console.log("合格");
  20. break;
  21. case score >= 80 && score <= 100:
  22. console.log("学霸");
  23. break;
  24. // 判断成绩是否合法
  25. case score > 100 || score < 0:
  26. console.log("非法数据,请输入0--100之间的数值");
  27. break;
  28. default:
  29. console.log("补考");
  30. }

6、三元运算

  1. // 三元运算符,条件? true : false
  2. // 相当于简单的if()else()语句,在双分支的情况下,简化代码
  3. //有什么限制,还不知道???猜想的限制可能是,应用于双分之以上的条件判断,
  4. //会导致代码可读性非常差
  5. score = 80;
  6. console.log(score >= 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