ECMAScript 6 引入了许可声明。

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

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


当前回答

在基本上,

for (let i = 0; i < 5; i++) {
  // i accessible ✔️
}
// i not accessible ❌

for (var i = 0; i < 5; i++) {
  // i accessible ✔️
}
// i accessible ✔️

<unk>️ Sandbox 要玩 ↓

此分類上一篇

其他回答

函数 VS 区块范围:

与 var 和 let 的主要区别是,与 var 宣言的变量是函数分解的,而与 let 宣言的函数是区块分解的。

function testVar () {
  if(true) {
    var foo = 'foo';
  }

  console.log(foo);
}

testVar();  
// logs 'foo'


function testLet () {
  if(true) {
    let bar = 'bar';
  }

  console.log(bar);
}

testLet(); 
// reference error
// bar is scoped to the block of the if statement 

当第一个函数测试Var 被称为变量 foo 时,与 var 声明,仍然在 if 声明之外可用。

与Let的变量:

当第二个函数测试Let被称为变量栏,用Let表示,仅可在 if 声明中访问,因为用Let表示的变量是区块分开的(其中一个区块是曲线条之间的代码,例如,如果{},为{},函数{})。

请不要让变量变量:

变量与不要被抓住:

console.log(letVar);

let letVar = 10;
// referenceError, the variable doesn't get hoisted

与 var do 相容的变量:

console.log(varVar);

var varVar = 10;
// logs undefined, the variable gets hoisted

var bar = 5;
let foo  = 10;

console.log(bar); // logs 5
console.log(foo); // logs 10

console.log(window.bar);  
// logs 5, variable added to window object

console.log(window.foo);
// logs undefined, variable not added to window object

显然,至少在Visual Studio 2015中,TypeScript 1.5,“var”允许一个区块中的相同变量名称的多个声明,而“Let”不。

这不会导致编译错误:

var x = 1;
var x = 2;

这将是:

let x = 1;
let x = 2;

此分類上一篇: <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个。

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

在2015年之前,使用 var 关键字是宣布 JavaScript 变量的唯一方式。

在 ES6 (JavaScript 版本) 之后,它允许 2 个新的关键字 let & const。

let = 可以重新分配 const = 不能重新分配 const ( const 源于常态,短形的“const” )

例子:

假设,宣布一个国家名称 / 你的母亲名称, const 是最合适的这里. 因为有更少的机会改变一个国家名称或你的母亲名称很快或晚些时候. 你的年龄,体重,工资,自行车的速度和更多类似于这些类型的数据,经常改变或需要重新分配。

使用 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写了一篇非常复杂的文章,他认为为什么不会这样。

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

此分類上一篇


如何跟踪浏览器支持