Blogger Information
Blog 26
fans 0
comment 3
visits 17742
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0105作业
后网络时代
Original
749 people have browsed it

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

答:值传递与引用传递使用场景有两种:
值传递:
简单数据类型(int , string, bool …)赋值传递:

  1. let a=1;
  2. let b=a;
  3. a=100;
  4. console.log(a);//=>100
  5. console.log(b);//=>1

数据单元是独享的
引用传递的数据类型是对象和数组:

  1. let o={a:1,b:2};
  2. let oo=o;
  3. console.log(oo);//=>{a:1,b:2}
  4. oo.a=100;
  5. console.log(o);//=>{a:100,b:2}//证明引用传递发生
  6. console.log(oo);//=>{a:100,b:2}//证明引用传递发生

注意:
demo1:

  1. oo={};//值传递
  2. console.log(o);//=>{a:100,b:2}
  3. demo2:
  4. o={};//值传递
  5. console.log(oo);//=>{a:100,b:2}

参数传递:
赋值传递参数:进行的是值传递

  1. let sum=a=>(a=10);
  2. let y=100;
  3. sum(y);//=>10
  4. console.log(y);//=>100

引用传递参数:

  1. let sum=x=>(x.a=1000);
  2. let y={a:1,b:2};
  3. sum(y);//=>{a:1000,b:2}
  4. console.log(y);//=>{a:1000,b:2} //引用传值发生

再看:

  1. let sum=x=>(x={});
  2. let y={a:1,b:2};
  3. sum(y);//=>{}//单独指向,没有影响到引用值
  4. console.log(y);//=>{a:1,b:2} //保持不变

但是引用传值已经发生
和老师上课讲的不一致

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

答:解构赋值:快速从集合数据中解析出独立变量
数组解构:

  1. let [a,b,c]=[1,2,3];
  2. console.log('a=%d,b=%d,c=%',a,b,c);
  3. let [a,b,c,d=[1,2,3,4,5
  4. cosnole.log(a,b,c,d);
  5. let [a,b,c]=[1,2,3];
  6. console.log('a=%d,b=%d,c=%',a,b,c);
  7. let [a,b]=[1,2,3];
  8. console.log(a,b);
  9. let [a,b,c,d]=[1,2,3];
  10. console.log(d)//=>未定义
  11. let [a,b,c,d='****']=[1,2,3];
  12. console.log(a,b,c,d)//=>1,2,3,****
  13. let [a,b,...c]=[1,2,3,4,5,6];
  14. console.log(a,b,c)//=>1,2,[3,4,5,6]
  15. let [,,a,,,]=[1,2,3,4,5,6];
  16. console.log(a)//=>3
  17. let x=1,y=2;
  18. [y,x]=[x,y]
  19. console.log(x,y);//实现x,y交换

对象解构:变量名要存在解构对象的属性名中。

  1. let {nameemail}={name:'zhangsan',email:'admin@126.com'}
  2. console.log(name,email);//=> zhangsan admin@126.com

重新开始:

  1. let email="123456@qq.com";
  2. let {name,email}={name:"zhangsan",email:"admin@qq.com"};
  3. //=>报错,email重复定义
  4. ({name,email}={name:"zhangsan",email:"admin@qq.com"});//强制定义
  5. 重新开始:
  6. let email="123456@qq.com";
  7. let {name,emailuserEmail}={name:"zhangsan",email:"admin@qq.com"};//定义别名解构

函数参数解构:
数组传参数的结构

  1. let sum=([a,b])=>a+b;
  2. console.log(sum([10,20]));

对象传参数的结构

  1. let getInfo=({name,age})=>[name,age];
  2. console.log(getInfo({name:'zhangsan',age:30}));

3. call,apply,bind的区别与联系,并试着实例演示一下

答:call()、apply()、bind() 都是用来重定义 this 的指向。
call,apply很类似,绑定后立即执行。bind绑定后返回函数声明,bind和call的传输参数一样,是多个的用逗号隔开,apply的参数则传递数组。

bind:

  1. function my(name){
  2. this.name=name;
  3. console.log(this.name);
  4. }
  5. let y=my.bind({name:'例子'},'bind绑定')
  6. y();//=>bind绑定
  7. call:
  8. my.call({name:'例子'},'call绑定')//=>call绑定
  9. apply:
  10. my.apply({name:'例子'},['apply绑定'])//=>apply绑定

4. 访问器属性的原理与实现过程,并实例演示

答:访问器属性就是把方法伪造成属性进行访问,通过get,set来设置。
例子:

  1. const product={
  2. data:[
  3. {name:'小说',price:100,num:10},
  4. {name:'科幻',price:200,num:20},
  5. {name:'宇宙说',price:100,num:55},
  6. {name:'原子理论',price:300,num:1},
  7. ],
  8. //访问器具属性
  9. get total(){
  10. return this.data.reduce((t,c)=>(t+=t+c.price*c.num),0)
  11. },
  12. set update(value){
  13. this.data[0].price=value;
  14. }
  15. }
  16. product.total//=>35300
  17. product.update=10

注意点:
访问器属性的名称和变量名冲突时,优先级:
访问器属性的优先级高于普通属性

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

答:

  1. let mark=100
  2. switch(true){
  3. case mark>=60 && mark<=80:
  4. console.log('及格');
  5. break
  6. case mark>80 && mark<=100:
  7. console.log('学霸');
  8. break
  9. case mark>100:
  10. console.log('非法数据');
  11. break
  12. default:
  13. console.log('补考');
  14. }

6. 三元运算解决了什么问题,有什么限制?

答:简化了分支语句,返回结果:相对于if…else ,需要三个操作数。

Correction status:Uncorrected

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