ECMAScript 6 引入了许可声明。

我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。

什么是差异?什么时候应该被允许使用而不是 var?


当前回答

下面表明“让我们”和“存在”在范围内是如何不同的:

let gfoo = 123;
if (true) {
    let gfoo = 456;
}
console.log(gfoo); // 123

var hfoo = 123;
if (true) {
    var hfoo = 456;
}
console.log(hfoo); // 456

gfoo,定义为 let 最初是在全球范围内,当我们宣布 gfoo 再次在如果条款的范围改变,当一个新的值被分配到变量在该范围内,它不会影响全球范围。

虽然 hfoo,由 var 定义,起初在全球范围内,但再次当我们在 if 条款内宣布它时,它考虑到全球范围 hfoo,尽管 var 已再次被用来宣布它。

其他回答

下面表明“让我们”和“存在”在范围内是如何不同的:

let gfoo = 123;
if (true) {
    let gfoo = 456;
}
console.log(gfoo); // 123

var hfoo = 123;
if (true) {
    var hfoo = 456;
}
console.log(hfoo); // 456

gfoo,定义为 let 最初是在全球范围内,当我们宣布 gfoo 再次在如果条款的范围改变,当一个新的值被分配到变量在该范围内,它不会影响全球范围。

虽然 hfoo,由 var 定义,起初在全球范围内,但再次当我们在 if 条款内宣布它时,它考虑到全球范围 hfoo,尽管 var 已再次被用来宣布它。

使用 var 声明定义的变量在其定义的整个函数中已知,从函数的开始。 (*) 使用 let 声明定义的变量仅在其定义的区块中已知,从其定义的时刻开始。

// i IS NOT known here
// j IS NOT known here
// k IS known here, but undefined
// l IS NOT known here

function loop(arr) {
    // i IS known here, but undefined
    // j IS NOT known here
    // k IS known here, but has a value only the second time loop is called
    // l IS NOT known here

    for( var i = 0; i < arr.length; i++ ) {
        // i IS known here, and has a value
        // j IS NOT known here
        // k IS known here, but has a value only the second time loop is called
        // l IS NOT known here
    };

    // i IS known here, and has a value
    // j IS NOT known here
    // k IS known here, but has a value only the second time loop is called
    // l IS NOT known here

    for( let j = 0; j < arr.length; j++ ) {
        // i IS known here, and has a value
        // j IS known here, and has a value
        // k IS known here, but has a value only the second time loop is called
        // l IS NOT known here
    };

    // i IS known here, and has a value
    // j IS NOT known here
    // k IS known here, but has a value only the second time loop is called
    // l IS NOT known here
}

loop([1,2,3,4]);

for( var k = 0; k < arr.length; k++ ) {
    // i IS NOT known here
    // j IS NOT known here
    // k IS known here, and has a value
    // l IS NOT known here
};

for( let l = 0; l < arr.length; l++ ) {
    // i IS NOT known here
    // j IS NOT known here
    // k IS known here, and has a value
    // l IS known here, and has a value
};

loop([1,2,3,4]);

// i IS NOT known here
// j IS NOT known here
// k IS known here, and has a value
// l IS NOT known here


今天使用安全吗?

有些人会说,在未来,我们只会使用让陈述,而这些陈述会变得过时。JavaScript老师Kyle Simpson写了一篇非常复杂的文章,他认为为什么不会这样。

事实上,我们实际上需要问自己是否安全使用放弃声明,这个问题的答案取决于你的环境:

此分類上一篇


如何跟踪浏览器支持


此分類上一篇: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> 點擊每個數字將登錄到主機:</p> <div id="div1">1</div> <div id="div2">2</div> <div id="div3">3</div> <div id="div4">4</div> <div id="div5">5</div>

每个单点处理器都会提到相同的对象,因为只有一个对象对象持有6个,所以你每次点击获得6个。

一个通用工作是将此插入一个匿名功能,并将它作为一个论点。 这种问题现在也可以通过使用,而不是在下面的代码中显示的变化来避免。

以下两个功能可以显示差异:

function varTest() {
    var x = 31;
    if (true) {
        var x = 71;  // Same variable!
        console.log(x);  // 71
    }
    console.log(x);  // 71
}

function letTest() {
    let x = 31;
    if (true) {
        let x = 71;  // Different variable
        console.log(x);  // 71
    }
    console.log(x);  // 31
}

ECMAScript 6 添加了另一个关键字来宣布变量,而不是“放下”。

“Let”和“Let”在“var”上引入的主要目标是,而不是传统的词汇,区块点点点点点点点点点点点点点点点点点点点点点点点点。