let和const

发布时间:2019-08-11 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了let和const脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
发布自Kindem的博客,欢迎大家转载,但是要注意注明出处。另外,该文章收纳在Kindem的个人的 IT 知识整理仓库,欢迎 Star、Fork、投稿

let

let是在ES6加入的新的变量声明方法,let声明变量的方法和var类似:

let a = 'hello';
VAR b = 'hello';

let的功能是声明一个作用域被限制在块级的变量,而var声明的变量的作用域只能是全局的或者整个函数块的

function vartest() {
    var x = 1;
    if (true) {
        var x = 2;
        // 2
        console.LOG(x);
    }
    // 2
    console.log(x);
}

function letTest() {
    let x = 1;
    if (true) {
        let x = 2;
        // 2
        console.log(x);
    }
    // 1
    console.log(x);
}

再举一个例子:

var a = 1;
var b = 2;

if (a === 1) {
    var a = 11;
    let b = 22;

    // 11
    console.log(a);
    // 22
    console.log(b);
}

// 11
console.log(a);
// 2
console.log(b);

另外,如果作用域位于程序的顶层,var会挂载到window上,而let不会:

var a = 'a';
let b = 'b';

// this -> window
// a
console.log(this.a);
// undefined
console.log(this.b);
// a
console.log(window.a);
// undefined
console.log(window.b);

在相同的函数或块作用域内重新声明同一个变量会引发一个重复定义的SyntaxError

if (x) {
    let foo;
    // SyntaxError
    let foo;
}

letvar都会在声明所在的作用域顶部被创建,这被称为变量提升,但是不同的是var的创建会伴随着一个undefined值,在赋值之后才会改变,而let没有被赋值之前是不会被初始化的,如果在这期间引用let声明的变量,会导致一个ReferenceError,直到初始化之前,该变量都处于暂存死区:

function test() {
    // undefined
    console.log(bar);
    // ReferenceError
    console.log(foo);
    var bar = 1;
    let foo = 2;
}
test();

两个复杂一点的例子:

function test(){
    var foo = 33;
    if (true) {
        // ReferenceError
        let foo = (foo + 55);
    }
}
test();
function go(n) {
    // Object {a: [1,2,3]}
    console.log(n);

    // ReferenceError
    for (let n of n.a) {
        console.log(n);
    }
}
go({a: [1, 2, 3]});

const

const的基本作用是声明一个作用域被限制在块级的常量,其作用域和let一样,基本使用也和let类似,但是const的特点是const声明的值一经创建无法被改变

使用const会创建一个值的只读引用,这意味着const声明的对象本省的引用是无法被改变的,但是其属性是可以被改变的,因为改变其属性并不会引起其引用的变化

下面给出const的一些特性的例子:

基本使用:

const a = 'abc';

无法被重复定义:

const a = 'abc';
// SyntaxError: Identifier 'a' has already been declared
const a = 'abc';

声明时就必须赋值:

// SyntaxError: Missing initializer in const declaration
const a;

无法被修改:

const a = 'a';
// TyPEError: Assignment to constant variable
a = 'b';

块级作用域:

if (true) {
    var a = 'a';
    const b = 'b';
    // a
    console.log(a);
    // b
    console.log(b);
}
// a
console.log(a);
// ReferenceError: not defined
console.log(b);

作用域在程序顶层时不会挂在window对象上:

var a = 'a';
const b = 'b';

// this -> window
// a
console.log(this.a);
// undefined
console.log(this.b);
// a
console.log(window.a);
// undefined
console.log(window.b);

脚本宝典总结

以上是脚本宝典为你收集整理的let和const全部内容,希望文章能够帮你解决let和const所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。