let 命令总结

作者 : 开心源码 本文共1133个字,预计阅读时间需要3分钟 发布时间: 2022-05-13 共311人阅读

1、基础语法

    let a = value    const b = value

语法相似 var

2、进阶语法

  • ES6 允许按照肯定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring)。
  • 这种写法属于“模式匹配”,只需等号两边的模式相同,左边的变量就会被赋予对应的值。下面是少量使用嵌套数组进行解构的例子。
  • 假如等号右边是数值和布尔值,则会先转为对象。
    let [a, b, c] = ['a', 'b', 'c']        console.log(a);     //  'a'    console.log(b);     //  'b'    console.log(c);     //  'c'            let {a1, b1, c1} = {a1: 'a', b1: 'b', c1: 'c'};        console.log(a1);     //  'a'    console.log(b1);     //  'b'    console.log(c1);     //  'c'            let [a2, b2, c2, d2, e2, f2] = "string"        console.log(a2);     //  's'    console.log(b2);     //  't'    console.log(c2);     //  'r'        console.log(d2);     //  'i'        console.log(e2);     //  'n'        console.log(f2);     //  'g'               let {length} = 'hello';        console.log(length);        // 5            let {toString} = 123;    toString === Number.prototype.toString // true

3、注意事项

块级作用域

什么是块级作用域?
一个 {} 就是一个块级作用域

for循环的计数器,就很合适使用let命令。

for (let i = 0; i < 10; i++) {  // i 只在for的循环体里有效}console.log(i);     //  Uncaught ReferenceError: i is not defined

无变量提升

它所公告的变量肯定要在公告后使用,否则报错。

console.log(a);     //  Uncaught ReferenceError: a is not definedlet a = 1;

有暂时性死区

只需块级作用域内存在let命令,它所公告的变量就“绑定”(binding)这个区域,不再受外部的影响。

let a = true;if (a) {    console.log(a);     //  Uncaught ReferenceError: a is not defined    let a = 1;}

不许重复公告

    let a = 0;    let a = 2;  // Uncaught SyntaxError: Identifier 'a' has already been declared
    let a = 0;    var a = 1;  // Uncaught SyntaxError: Identifier 'a' has already been declared
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » let 命令总结

发表回复