ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
当前回答
一些黑客与Let:
1。
let statistics = [16, 170, 10];
let [age, height, grade] = statistics;
console.log(height)
二。
let x = 120,
y = 12;
[x, y] = [y, x];
console.log(`x: ${x} y: ${y}`);
3、
let node = {
type: "Identifier",
name: "foo"
};
let { type, name, value } = node;
console.log(type); // "Identifier"
console.log(name); // "foo"
console.log(value); // undefined
let node = {
type: "Identifier"
};
let { type: localType, name: localName = "bar" } = node;
console.log(localType); // "Identifier"
console.log(localName); // "bar"
Getter 和 Setter 與 Let:
let jar = {
numberOfCookies: 10,
get cookies() {
return this.numberOfCookies;
},
set cookies(value) {
this.numberOfCookies = value;
}
};
console.log(jar.cookies)
jar.cookies = 7;
console.log(jar.cookies)
其他回答
下面是让关键词的解释,有几个例子。
主要区别在于,一个变量的范围是整个关闭功能。
此图在维基百科上显示哪些浏览器支持JavaScript 1.7.
请注意,只有 Mozilla 和 Chrome 浏览器支持它. IE、Safari 和其他可能不支持它。
var 是全球范围(可容量)的变量。
Let and const 是区块范围。
测试JS
{ let l = 'let'; const c = 'const'; var v = 'var'; v2 = 'var 2'; } console.log(v, this.v); console.log(v2, this.v2); console.log(l); // ReferenceError: l 未定义 console.log(c); // ReferenceError: c 未定义
现在我认为有更好的转换变量到一个区块的声明使用允许:
function printnums()
{
// i is not accessible here
for(let i = 0; i <10; i+=)
{
console.log(i);
}
// i is not accessible here
// j is accessible here
for(var j = 0; j <10; j++)
{
console.log(j);
}
// j is accessible here
}
我认为人们会开始使用Let Here之后,以便他们在JavaScript中与其他语言、Java、C#等类似的分解。
没有对JavaScript的漏洞的明确理解的人早就犯了错误。
Hoisting 不支持使用Let。
在此方法中,在JavaScript中存在的错误正在被删除。
提到ES6在深度:让它更好地理解。
显然,至少在Visual Studio 2015中,TypeScript 1.5,“var”允许一个区块中的相同变量名称的多个声明,而“Let”不。
这不会导致编译错误:
var x = 1;
var x = 2;
这将是:
let x = 1;
let x = 2;
差异在于与每一个声明的变量范围。
让变量仅在其最接近的封锁区({... })中可见。让变量仅在变量被宣布后发生的代码线上可用。让变量不被随后的变量或变量重新宣布。让变量不被全球变量添加到全球窗口对象中。让变量易于使用与封锁(它们不会导致赛车条件)。
{
let x = 1;
}
console.log(`x is ${x}`); // ReferenceError during parsing: "x is not defined".
{
x = x + 1; // ReferenceError during parsing: "x is not defined".
let x;
console.log(`x is ${x}`); // Never runs.
}
没有重新宣言:下列代码表明,被允许宣言的变量不能随后重新宣言:
let x = 1;
let x = 2; // SyntaxError: Identifier 'x' has already been declared
var button = "I cause accidents because my name is too common.";
let link = "Though my name is common, I am harder to access from other JS files.";
console.log(link); // OK
console.log(window.link); // undefined (GOOD!)
console.log(window.button); // OK
5. 易于使用与关闭: 与 var 表示的变量与关闭内部的关闭工作不太好. 这里是一个简单的旋转,结果的值序列,变量 i 有在不同的时间点:
for (let i = 0; i < 5; i++) {
console.log(`i is ${i}`), 125/*ms*/);
}
具体而言,这些输出:
i is 0
i is 1
i is 2
i is 3
i is 4
在JavaScript中,我们经常使用变量比它们创建时更晚的时间。当我们通过延迟输出以关闭转移到设置Timeout时:
for (let i = 0; i < 5; i++) {
setTimeout(_ => console.log(`i is ${i}`), 125/*ms*/);
}
for (var i = 0; i < 5; i++) {
setTimeout(_ => console.log(`i is ${i}`), 125/*ms*/);
}
i is 5
i is 5
i is 5
i is 5
i is 5