Blogger Information
Blog 55
fans 3
comment 0
visits 54663
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
let、const与解构赋值
王佳祥
Original
1098 people have browsed it

let、const与解构赋值

一、let

  • es6新增了let命令,用来声明变量,它的用法类似var,但是所声明的变量只在let命令所在的代码块内有效。
  1. {
  2. let a = 'let变量';
  3. var b = 'var变量';
  4. }
  5. console.log(a);//a is not defined
  6. console.log(b);//正确输出
  1. 上面代码块,分别用letvar声明了两个变量,在代码块之外调用这两个变量时,let声明的变量就会报错,而var声明的变量正常输出,这说明了let只在它所在的代码块内有效。
  • let禁止重复声明
  1. //let声明变量
  2. let num;
  3. //禁止重复声明
  4. let num;//会报错
  • 不存在变量声明提升
  1. //不存在变量声明提升
  2. console.log(username);//会报错
  3. let username = "wang";
  4. function fn() {
  5. console.log(username);
  6. let username = "jack";
  7. }
  8. console.log(fn());//会报错
  • 支持块作用域
  1. //支持块作用域
  2. if (true) {
  3. var price = 99;
  4. //let price = 50;
  5. }
  6. console.log(price);
  7. let lis = document.querySelectorAll("li");
  8. for (let i = 0; i < lis.length; i++) {
  9. lis[i].addEventListener("click", function () {
  10. console.log("点击了第" + i + "个");
  11. });
  12. }


二、const

  • const声明只读变量:常量
  1. //const声明只读变量:常量
  2. //1.const常量,在一个脚本的声明周期内,禁止更新,所以在声明时必须初始化
  3. const NATION = "中国";
  4. //2.不允许重复声明
  5. //const NATION ="CHINA";
  6. //3.支持块作用域
  7. if (true) {
  8. const EMAIL = "wukong@qq.com";
  9. console.log(EMAIL);
  10. }
  11. //console.log(EMAIL);会报错
  12. /*
  13. let 与 const相同之处
  14. 1.禁止重复声明
  15. 2.支持块作用域
  16. 不同之处,let允许更新,而const禁止更新
  17. 一种风格就是全部都用const,除非这个数据后期会有改变
  18. 可能会更新的或常用的都使用let,除非不会或不太可能被修改的采用const
  19. 有一些数据类型强烈推荐使用const
  20. 对象或数组
  21. */
  22. const arr = [1, 2, 3];
  23. console.log(arr);
  24. arr[1] = 20;
  25. console.log(arr);
  26. //arr = ["a","b"]这样更新会报错
  27. const obj = { x: "red", y: "green" };
  28. console.log(obj);
  29. obj.x = "blue";
  30. console.log(obj);
  31. //obj = {};//这样更新会报错,不能直接赋值
  32. const body = document.body;
  33. body.style.backgroundColor = "yellow";

三、var/let/const在全局作用域绑定上的区别

  1. //var 全局
  2. var email = "wukong@qq.com";
  3. console.log(email);
  4. console.log(window.email);
  5. var email = "bajie@qq.com";
  6. console.log(email);
  7. //污染全局环境
  8. //尽量不要创建全局变量,可以创建临时变量
  9. //var role = "custom";
  10. //临时变量就是创建立即执行函数
  11. (function () {
  12. var role = "custom";
  13. })();
  14. //这样就不会显示在全局环境中
  15. //在当前全局环境中创建一个命名空间
  16. //如果在函数对象上添加就是静态成员
  17. var ns = {};
  18. ns.id = 10;
  19. ns.username = "wang";
  20. ns.email = "wang@qq.com";
  21. console.log(window.ns.id, window.ns.username, window.ns.email);
  22. /*------------------------------------------------------*/
  23. //let
  24. let colors = ["red", "green", "blue"];
  25. const BRAND = "HUAWEI";
  26. console.log(colors, BRAND);
  27. //使用let/const声明的变量不会成为全局对象window的属性
  28. console.log(window.colors, window.BRAND); //返回undefined
  29. var name = "hello world";
  30. let name = "hello php";
  31. //let和const声明的变量只会出现在词法作用域/块作用域/静态作用域中
  32. //在全局环境下,var,let,const行为类似

四、解构

  1. //解构主要是针对对象和数组
  2. const obj = { x: 1, y: 2, z: 3 };
  3. /* for (k in obj) {
  4. console.log(k);
  5. } */
  6. //模板解构
  7. //对象
  8. const { x, y, z } = { x: 1, y: 2, z: 3 };
  9. console.log(x, y, z);
  10. //数组
  11. const [a, b, c] = ["a", "b", "c"];
  12. console.log(a, b, c);
  13. //解构的目的:将集合数据按规则打散到一些独立的变量中
  14. //一.传统方式
  15. const product = {
  16. name: "电脑",
  17. price: 5000,
  18. };
  19. let name = product.name;
  20. let price = product.price;
  21. console.log("%s:%c %d", name, "color:red", price);
  22. //二.对象解构
  23. const user = {
  24. username: "wang",
  25. email: "wang@qq.com",
  26. };
  27. //解构语法:解构变量声明 = 要被解构的数据
  28. //let { username, email } 就是结构变量声明
  29. let { username, email } = {
  30. username: "wang",
  31. email: "wang@qq.com",
  32. };
  33. console.log("%s (%s)", username, email);
  34. //1.对象的属性名必须与左边解构变量声明模板中的变量名同名
  35. //2.解构变量必须初始化
  36. let { q, w } = {};
  37. //3.可以将左侧的变量声明看成右侧的赋值模板
  38. //三.解构表达式
  39. let a1 = 10;
  40. let b1 = 20;
  41. console.log(a1, b1);
  42. a1 = 150;
  43. b1 = 300;
  44. console.log(a1, b1);
  45. //换种方式来更新
  46. //1.场景1:用来更新变量
  47. ({ a1, b1 } = { a1: 15, b1: 60 });
  48. console.log(a1, b1);
  49. //2.场景2
  50. function out({ m: x, n: y }) {
  51. console.log(x, y);
  52. x = 20;
  53. y = 40;
  54. console.log(x, y);
  55. }
  56. out({ m: 8, n: 8 });
  57. //四.解构声明中设置默认值
  58. let stu = {
  59. stuName: "wang",
  60. gender: "male",
  61. };
  62. let { stuName, gender = "famale", age = 15 } = stu;
  63. console.log(stuName, gender, age);
  64. //五.解构声明中使用变量别名
  65. const book = {
  66. name: "ES6开发指南",
  67. price: 109,
  68. };
  69. let { name: bookname, price: bookprice } = book;
  70. console.log(bookname, bookprice);

五、嵌套对象解构

  1. //对象的属性值也可以是对象或其它复杂类型、
  2. const stu = {
  3. name: "wang",
  4. course: {
  5. php: {
  6. level: "basis",
  7. grade: 80,
  8. },
  9. front: {
  10. level: "advance",
  11. grade: 70,
  12. },
  13. },
  14. };
  15. console.log(stu);
  16. let {
  17. course: { php },
  18. } = stu;
  19. console.table(php);
  20. let {
  21. course: {
  22. php: { level },
  23. },
  24. } = stu;
  25. console.table(level);
  26. let {
  27. course: {
  28. front: { level: frontlevel },
  29. },
  30. } = stu;
  31. console.table(frontlevel);
  32. //多次解构
  33. let {
  34. course,
  35. course: { php: phpinfo },
  36. course: {
  37. php: { level: levelinfo },
  38. },
  39. } = stu;
  40. console.log(course, php, level);

六、数组解构

  1. //数组解构
  2. const [c1, c2, c3] = ["上海", "北京", "广州"];
  3. console.log(c1, c2, c3);
  4. //1.数组解构声明
  5. let user = [1, "admin", "admin@qq.com"];
  6. let [id, name, email] = user;
  7. console.log(id, name, email);
  8. let [, username, ,] = user;
  9. //数组解构和对象解构必须要初始化
  10. console.log(name);
  11. //2.数组解构表达式:更新数组元素
  12. let lesson = "es6";
  13. let grade = 60;
  14. //不要加let,不要加括号([lesson, grade] = ["php", 90]);
  15. [lesson, grade] = ["php", 90];
  16. console.log(lesson, grade);
  17. //3.使用默认值
  18. let [brand, model, color = "red"] = ["huawei", "p40"];
  19. console.log(brand, model, color);
  20. //4.在函数参数中也可以使用数组解构
  21. function add([x, y = 100]) {
  22. return x + y;
  23. }
  24. console.log(add([50, 100]));
  25. console.log(add([100]));
  26. //5.数组嵌套解构
  27. let [a1, a2, [a3, a4], a5] = [10, 20, [30, 40], 50];
  28. console.log(a1, a2, a3, a4, a5);

七、学习总结

1.let

  • 禁止重复声明

  • 不存在变量提升

  • 支持块作用域

2.const

  • const声明只读变量:常量

  • const声明变量必须初始化

  • 禁止重复声明

  • 支持块作用域

  • 禁止更新

  • 可能会更新的或常用的都使用let,除非不会或不太可能被修改的采用const

3.var/let/const在全局作用域绑定上的区别

  • 使用var声明的变量会出现在全局环境中

  • 可以用立即执行函数来创建临时变量,这样就不会污染全局环境

  1. (function () {
  2. var role = "custom";
  3. })();
  • 也可以创建一个命名空间
  1. var ns = {};
  2. ns.id = 10;
  3. ns.username = "wang";
  4. ns.email = "wang@qq.com";
  5. console.log(window.ns.id, window.ns.username, window.ns.email);
  • 使用let/const声明的变量不会成为全局对象window的属性
  • let和const声明的变量只会出现在词法作用域/块作用域/静态作用域中

4.解构

  • 解构主要是针对对象和数组

  • 解构的目的:将集合数据按规则打散到一些独立的变量中

  • 解构语法:解构变量声明 = 要被解构的数据

  1. let { username, email } = {username: "wang",email: "wang@qq.com",};
  • 对象的属性名必须与左边解构变量声明模板中的变量名同名

  • 解构变量必须初始化

  • 解构表达式可以用来更新变量

  1. let a1 = 10;
  2. let b1 = 20;
  3. ({ a1, b1 } = { a1: 15, b1: 60 });
  4. console.log(a1, b1);
  • 解构声明中可以设置默认值
  1. let stu = {stuName: "wang",gender: "male",};
  2. let { stuName, gender = "famale", age = 15 } = stu;
  3. console.log(stuName, gender, age);
  • 解构声明中可以使用变量别名
  1. const book = {name: "ES6开发指南",price: 109,};
  2. let { name: bookname, price: bookprice } = book;
  3. console.log(bookname, bookprice);

5.嵌套对象解构

  • 对象的属性值也可以是对象或其它复杂类型
  1. const stu = {name: "wang",
  2. course: {
  3. php: {level: "basis",grade: 80,},
  4. front: {level: "advance",grade: 70,},
  5. },
  6. };
  7. let {course: { php },} = stu;
  8. console.table(php);
  9. let { course: { php: { level },},} = stu;
  • 多次解构
  1. let {
  2. course,
  3. course: { php: phpinfo },
  4. course: { php: { level: levelinfo },},
  5. } = stu;
  6. console.log(course, php, level);

6.数组解构

  • 数组解构和对象解构必须要初始化

  • 数组解构表达式:可以更新数组元素

  1. let lesson = "es6";
  2. let grade = 60;
  3. //不要加let,不要加括号([lesson, grade] = ["php", 90]);
  4. [lesson, grade] = ["php", 90];
  5. console.log(lesson, grade);
  • 也可以使用默认值
  1. let [brand, model, color = "red"] = ["huawei", "p40"];
  2. console.log(brand, model, color);
  • 在函数参数中也可以使用数组解构
  1. function add([x, y = 100]) {
  2. return x + y;
  3. }
  4. console.log(add([50, 100]));
  5. console.log(add([100]));
  • 数组嵌套解构
  1. let [a1, a2, [a3, a4], a5] = [10, 20, [30, 40], 50];
  2. console.log(a1, a2, a3, a4, a5);
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