ECMAScript 6 引入了许可声明。

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

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


当前回答

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

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

此分類上一篇


如何跟踪浏览器支持


答答答答答答答答答答答答答答答答答

var 变量是全球性的,基本上可以到达到任何地方,而让变量不是全球性的,只有直到一个关闭的偏见杀死它们。

请参见下面的我的例子,并注意狮子(Let)变量如何在两个 console.logs 中以不同的方式行事;它在第二个 console.log 中变得无效。

var cat = "cat";
let dog = "dog";

var animals = () => {
    var giraffe = "giraffe";
    let lion = "lion";

    console.log(cat);  //will print 'cat'.
    console.log(dog);  //will print 'dog', because dog was declared outside this function (like var cat).

    console.log(giraffe); //will print 'giraffe'.
    console.log(lion); //will print 'lion', as lion is within scope.
}

console.log(giraffe); //will print 'giraffe', as giraffe is a global variable (var).
console.log(lion); //will print UNDEFINED, as lion is a 'let' variable and is now out of scope.

如上所述:

差异是分解. var 被分解到最接近的函数区块,然后被分解到最接近的封锁区块,这可能比函数区块小。

例子1:

在我的两个例子中,我有一个功能 myfunc. myfunc 包含一个变量 myvar 等于 10. 在我的第一个例子中,我检查 myvar 等于 10 (myvar==10). 如果是的话,我 agian 宣布一个变量 myvar (现在我有两个 myvar 变量) 使用 var 关键词并将其分配到一个新的值(20)。

此分類上一篇

例2:在我的第二個例子中,而不是在我的條件區域中使用 var 關鍵字,我宣告 myvar 使用 let 關鍵字. 現在,當我呼叫 myfunc 我得到兩個不同的出口: myvar = 20 和 myvar = 10.

因此,差异很简单,即它的范围。

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

这不会导致编译错误:

var x = 1;
var x = 2;

这将是:

let x = 1;
let x = 2;

const name = 'Max'; let age = 33; var hasHobbies = true; name = 'Maximilian'; age = 34; hasHobbies = false; const summarizeUser = (userName, userAge, userHasHobby) => { return ( 'Name is'+ userName + ', age is'+ userAge +'and the user has hobbies:'+ userHasHobby ); } console.log(summarizeUser(name, age, hasHobbies));

正如你可以从上面的代码运行中看到的那样,当你尝试更改 const 变量时,你会发现一个错误:

试图超越一个“名称”,这是一个恒定的。

TypeError: Invalid assignment to const 'name'.

但是,看看放变量。

首先,我们宣布让年龄=33岁,然后将另一个值年龄=34岁,这是OK;当我们试图改变时,我们没有任何错误。