Blogger Information
Blog 56
fans 1
comment 0
visits 62256
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
let ,const功能与应用场景对象解析与数组解构
零龙
Original
875 people have browsed it

let与const声明的变量的特点

  • let

  • 1,let定义的变量不会预解析,必须先声明再使用,否则会报错
  • 2,let不能定义已经定义过的变量(无论之前是用var定义的还是let或者const定义的)
  • 3,let是块级作用域,函数内部使用let定义后,对函数外部无影响,简单说就是在一个{}里面生效
  • 4,由于let是块级作用域,在循环绑定事件过程中let会在这个循环中生效,再次循环时let会重新定义生效
  • const

  • 1,const定义的变量不会预解析,必须先声明再使用,否则会报错
  • 2,const定义的变量不允许修改,但是,在数组里面,const的值是允许被修改的,这是因为const存储的是地址,值的内容可以变化

实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. //在学习的script中使用var定义变量
  12. console.log(a);
  13. var a = 12 ;
  14. //let用同样的方式就会报错
  15. // console.log(b);
  16. let b = 20;
  17. //报错:Uncaught ReferenceError: Cannot access 'b' before initialization
  18. //-----------------------------
  19. let c = 25;
  20. //let声明过的变量不能被重新赋值
  21. //报错:Uncaught SyntaxError: Identifier 'c' has already been declared
  22. //-----------------------------
  23. function num()
  24. {
  25. let d = 15 ;
  26. return (d * 3) + c;
  27. }
  28. console.log(num());
  29. //let作用域:在函数内可以调用函数外的let,反之外不能调用内
  30. //-----------------------------
  31. var ul = document.createElement("ul");
  32. ul.setAttribute("id","nav");
  33. document.body.appendChild(ul);
  34. for(let i = 1;i < 5; i++)
  35. {
  36. var lis =document.createElement("li");
  37. ul.appendChild(lis);
  38. console.log(i); // 正常输出1,2,3,4
  39. }
  40. // console.log(i); //报错:i is not defined
  41. //let 在使用循环定义变量很实用。
  42. //--------------------------------------
  43. //const声明不可以改变的变量、不能被修改
  44. const e = 100;
  45. console.log(e);
  46. // e = e-10; //报错:Assignment to constant variable.
  47. const arr = ['1','2','3','4','5'];
  48. arr[2]='我是数字';
  49. console.log(arr);
  50. //const 定义的数字是可以被修改的
  51. //暂时性死区:
  52. var v =12;
  53. if(true)
  54. {
  55. //console.log(v); //报错:Cannot access 'v' before
  56. const v = 24;
  57. console.log(v); //重新定义v,输出 24
  58. }
  59. console.log(v); //输出 12
  60. </script>
  61. </body>
  62. </html>

解析

  • 使用let对数组及对象解构,可以方便的设置和更新数组。

实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. //----------------数组解构----------------------------
  12. // let array = ['1','曹操','男','39','cc@qq.com','13999999999'];
  13. // let [id,user,sex,age,email,mobile] = array;
  14. // console.log(id);
  15. // console.log(user);
  16. // console.log(sex);
  17. // console.log(age);
  18. // console.log(email);
  19. //let 可以对数组属性赋值到变量中,数组按索引排列,自定义变量名
  20. //----------------数组解构1-1-----------------------------
  21. // let [username,[age1,sex],email] = ['曹操',[23,'男'],'cc@qq.com'];
  22. // console.log(username);
  23. //----------------数组解构-2-----------------------------
  24. // let array = ['1','曹操','男','39','cc@qq.com','13999999999'];
  25. // let [id,user,sex,age,email,mobile] = [array[0],array[1]='刘备',array[2],array[3],array[4]='lb@qq.com',array[5]='13888888888'];
  26. // console.log(user);
  27. // console.log(email);
  28. // console.log(mobile);
  29. //----------------数组解构-3------------------------------
  30. // let array = ['刘备','男','lb@qq.com'];
  31. // let [user,sex,mobile] = array;
  32. // console.log(array);
  33. //----------------对象解构------------------------------
  34. // let obj = {'user':'曹操','email':'cc@qq.com','mobile':'13999999999'};
  35. // let {user,email,mobile} = obj;
  36. // console.log(user);
  37. // console.log(email);
  38. // console.log(mobile);
  39. //----------------对象解构-1------------------------------
  40. // let user = {
  41. // userName :'刘备',
  42. // email:"lb@qq.com",
  43. // other:{
  44. // age:25,
  45. // sex:'男',
  46. // }
  47. // };
  48. // let {userName,email,other:{age,sex}} = user;
  49. // console.log(userName);
  50. // console.log(email);
  51. // console.log(age);
  52. //区分大小写,
  53. //--------------对象解构-2------------------------------
  54. // let user = '曹操';
  55. // let email = 'cc@qq.com';
  56. // ({user,email} = {user:'刘备',email:'lb@qq.com'});
  57. // console.log(user);
  58. // console.log(email);
  59. //--------------对象解构-3----------------------------------
  60. // let user = {
  61. // userName :'刘备',
  62. // email:"lb@qq.com",
  63. // other:{
  64. // age:undefined,
  65. // sex:'男',
  66. // }
  67. // };
  68. // let {userName,email,other:{age= 30,sex}} = user;
  69. // console.log(age);
  70. //--------------对象解构-4----------------------------------
  71. let user = {
  72. userName :'刘备',
  73. email:"lb@qq.com",
  74. other:{
  75. age:undefined,
  76. sex:'男',
  77. }
  78. };
  79. let {userName:User,email,other:{age=25,sex}}=user;
  80. console.log(User);
  81. console.log(sex);
  82. </script>
  83. </body>
  84. </html>

示例图

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