Blogger Information
Blog 45
fans 0
comment 0
visits 34639
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
解构赋值与流程控制
咸鱼老爷
Original
528 people have browsed it

值传递与引用传递

  • 1.赋值
  • 1.1 值传递:原始类型,string,number,bool
    1. let a = 1;
    2. let b = a;
    3. console.log('a=%d,b=%d', a, b);
    4. // 更新a,b不受影响
    5. a = 2;
    6. console.log('a=%d,b=%d', a, b);
  • 1.2 引用传递:引用类型,object,array
    1. let obj1 = {
    2. a: 1,
    3. b: 2
    4. };
    5. console.log(obj1);
    6. let obj2 = obj1;
    7. console.log(obj2);
    8. // 更新obj1
    9. obj1.a = 10;
    10. obj1.b = 11;
    11. console.log(obj1);
    12. //obj2同步更新
    13. console.log(obj2);
  • 2.传参
    传参时,不论什么类型,都是值传递
    入参:调用函数是传入的参数。函数中对参数的更新并不会影响到入参
    对于引用类型,只有全新赋值才算是更新,修改属性不算。
  • 3.深拷贝:值传递
  • 4.浅拷贝:引用传递

    模板字面量与标签函数

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

模板字面量使用反引号”`”;
模板字面量的组成:

  • 字符串字面量:+,=;
  • 变量表达式: a,b,(a+b);
    模板字面量创建多行字符串可以保留格式
    1. let menu = ['首页', '系统', '新闻', '产品'];
    2. let htmlStr = `
    3. <ul>
    4. <li>${menu[0]}</li>
    5. <li>>${menu[2]}</li>
    6. <li>>${menu[2]}</li>
    7. <li>>${menu[3]}</li>
    8. </ul>
    9. `;
    10. console.log(htmlStr);
  • 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 = 4] = [1, 2, 3]
    6. console.log(a, b, c, d);
    7. [a, b, ...c] = [1, 2, 3, 4, 5, 6, 7];
    8. console.log(a, b, c);
    9. //单独取一个值
    10. [, , c, ] = [1, 2, 6, 4]
    11. console.log(c);
    12. //交换
    13. [y,x]=[x,y];
  • 2、对象解构
    1. let {
    2. id,
    3. name
    4. } = {
    5. id: 10,
    6. name: '手机'
    7. };
    8. console.log(id, name);
    9. ({
    10. name,
    11. id
    12. } = {
    13. id: 10,
    14. name: '手机'
    15. });
    16. console.log(id, name);
    17. let email = 'admin@qq.com';
    18. let {
    19. role,
    20. //取别名
    21. email: userEmail
    22. } = {
    23. role: 'user',
    24. email: 'user@qq.com'
    25. };
    26. console.log(userEmail);
    27. console.log(email);
  • 3、参数解构
    数组传参
    1. let sum = ([a, b]) => a + b;
    2. console.log(sum([1, 2]));
    对象传参
    1. let getUser = ({
    2. name,
    3. email
    4. }) => [name, email];
    5. console.log(getUser({
    6. email: 'admin@admin.com',
    7. name: '系统管理员'
    8. }));

箭头函数中的this总是指向定义它时的作用域(静态作用域|词法作用域),而非调用时的作用域;

流程控制

  • 1、if else
    1. let score = 59;
    2. if (score >= 90) {
    3. console.log('优秀');
    4. } else if (score >= 80) {
    5. console.log('良好');
    6. } else if (score >= 70) {
    7. console.log('合格');
    8. } else if (score >= 60) {
    9. console.log('及格');
    10. } else {
    11. console.log('不及格');
    12. }
  • 2、switch 简化多分支的if else
    区间判断 switch(true);
    1. let score = 80;
    2. switch (true) {
    3. case score >= 90:
    4. console.log('优秀');
    5. break;
    6. case score >= 80:
    7. console.log('良好');
    8. break;
    9. case score >= 70:
    10. console.log('合格');
    11. break;
    12. case score >= 60:
    13. console.log('及格');
    14. break;
    15. default:
    16. console.log('补考');
    17. }
    单值
    1. let status = 'success';
    2. switch (status) {
    3. case 'ok':
    4. console.log('成功');
    5. break;
    6. case 'fail':
    7. console.log('失败');
    8. break;
    9. default:
    10. console.log('未知错误');
    11. }
  • 3、 三元运算符
    ?:
    1. let score=80;
    2. console.log(score>=80?'合格':'补考');
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