Blogger Information
Blog 37
fans 0
comment 0
visits 34807
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js中的传递,解构,this指向,访问器属性等介绍
手机用户1607314868
Original
724 people have browsed it

值传递与引用传递

1.值传递
值传递适用的数据类型为:原始类型包括string,number,bool等主要特征是互不影响,只是传递值

  1. let a=1;
  2. let b=a;
  3. console.log(a,b);
  4. a=2;
  5. //更新a,不影响b,b没有变化
  6. console.log(a,b);
  1. 引用传递
    引用传递适用的是引用类型包括:object,array.
  1. let obj1={a:1,b:2};
  2. let obj2=obj1;
  3. obj1.a=10;
  4. console.log(obj1);
  5. //obj2中的a值也变成了10
  6. console.log(obj2);

模板字面量与标签函数

1.模板字面量
将表达式嵌入到字符串。使用反引号``。

  1. let a=1;
  2. b=2;
  3. let res=a+"+"+b+"="+(a+b);
  4. console.log(res);
  5. //模板字面量
  6. res=`${a}+${b}=${a+b}`;
  7. console.log(res);

2.标签函数
自定义模板字面量的行为
使用自定义函数来处理模板字面量,它的参数约定
第一个参数:模板字面量中的字符串字面量组成的数组
从第二个参数开始,将模板字面量中的变量依次传入

  1. let sum=(strs,a,b)=>{
  2. console.log(strs);
  3. console.log(a,b);
  4. };
  5. sum`${a}+${b}=`;

解构

快速从集合数据(数组/对象)解构出独立变量

  • 数组
  1. let [a,b,c]=[1,2,3];
  2. console.log(a,b,c);
  • 对象
    1. let {id,name}={id:10,name:"电脑"};
    2. console.log(id,name);
    3. //属性名与变量名必须对应,顺序无所谓
    4. ({name,id}={name:"电脑",id:20});
  • 参数解构
    数组传参
  1. let sum=([a,b])=>a+b;
  2. console.log([1,2]);

对象传参

  1. let user=({name,id})=>({name,id});
  2. console.log(user({name:"你好",id=2}));

对象字面量的简化

  1. let user={
  2. userName:"老师",
  3. userEmail:"tp@php.cn",
  4. getInfo:function(){
  5. return `${this.userName}(${this.userEmail})`;
  6. }
  7. }
  8. console.log(user.getInfo());
  9. let {userName,userEmail}={userName:"师傅",userEmail:"qq@163.com"};
  10. user={
  11. //当属性名与同一个作用域的变量名一样,可以直接使用属性名来引用该变量的值
  12. userName,
  13. userEmail,
  14. getInfo(){
  15. return `${this.userName}(${this.userEmail})`;
  16. }
  17. }
  18. console.log(user.getInfo());
  19. user={
  20. userName,
  21. userEmail,
  22. getInfo:()=>`${this.userName}(${this.userEmail})`,
  23. //this指向了window因为对象没有作用域,所以会this会逐层搜寻直到window
  24. test:()=>console.log(this),
  25. }
  26. console.log(user.getInfo());

call,apply,bind

bind的作用就是将this指向改变。
call,apply跟bind的区别就是,bind只是返回一个函数声明,不会执行,而call,apply会立即执行

  1. <button>按钮</button>
  2. <script>
  3. function hello(name){
  4. this.name=name; console.log(this.name);
  5. }
  6. const obj={name:'admin'};
  7. //给 obj 绑定 hello函数 bind() 不会立即执行,只返回一个函数声明
  8. let f=hello.bind(obj,'同学');
  9. //加上()才会执行
  10. console.log(f());
  11. //call接收一个变量
  12. f=hello.call(obj,"数学老师");
  13. console.log(f);
  14. //apply接收一个数组参数
  15. f=hello.apply(obj,['语文老师']);
  16. console.log(f);

访问器属性

就是将对象里的方法变为属性
使用get和set来定义,get是得到属性的值,set设置属性的值

  1. const product={
  2. data:[
  3. {name:"手机",price:3000,num:3},
  4. {name:"电脑",price:5000,num:5},
  5. {name:"相机",price:7000,num:2},
  6. ],
  7. getAmounts(){
  8. return this.data.reduce((t,c)=>(t +=c.price * c.num),0);
  9. },
  10. }
  11. console.log("总额:",product.getAmounts());
  12. products={
  13. data:[
  14. {name:"手机",price:3000,num:3},
  15. {name:"电脑",price:5000,num:5},
  16. {name:"相机",price:7000,num:2},
  17. ],
  18. get getAmounts(){
  19. return this.data.reduce((t,c)=>(t+=c.price*c.num),0);
  20. },
  21. set getAmounts(price){
  22. this.data[1].price=price;
  23. }
  24. }
  25. products.getAmounts=3000;
  26. console.log(products.data[1].price);
  27. console.log("总额:",products.getAmounts);

流程控制

if分支是用来控制区间值,switch一般用来单值

  1. let a=2;
  2. if(a>=1){
  3. console.log('大了');
  4. }else{
  5. console.log('小了');
  6. }
  7. let b=success;
  8. switch(b){
  9. case "fail":console.log('失败');
  10. break;
  11. case "success":console.log('成功');
  12. break;
  13. default: console.log('平局');
  14. }

三元运算

方便简单简化代码。但只能用来控制简单的流程。

  1. let score=40
  2. (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
Author's latest blog post