ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
ECMAScript 6 引入了许可声明。
我听说它被描述为一个当地变量,但我仍然不确定它是如何行为不同于 var 关键词。
什么是差异?什么时候应该被允许使用而不是 var?
当前回答
答案缺少一点:
{
let a = 123;
};
console.log(a); // ReferenceError: a is not defined
其他回答
正如我目前正在试图深入了解JavaScript,我将分享我的简短研究,其中包含一些已经讨论的伟大作品,以及一些其他细节,从不同的角度。
理解VAR和LAT之间的差异可以更容易,如果我们理解函数和区块范围之间的差异。
让我们来看看以下案例:
(function timer() {
for(var i = 0; i <= 5; i++) {
setTimeout(function notime() { console.log(i); }, i * 1000);
}
})();
Stack VariableEnvironment //one VariablEnvironment for timer();
// when the timer is out - the value will be the same for each iteration
5. [setTimeout, i] [i=5]
4. [setTimeout, i]
3. [setTimeout, i]
2. [setTimeout, i]
1. [setTimeout, i]
0. [setTimeout, i]
####################
(function timer() {
for (let i = 0; i <= 5; i++) {
setTimeout(function notime() { console.log(i); }, i * 1000);
}
})();
Stack LexicalEnvironment - each iteration has a new lexical environment
5. [setTimeout, i] [i=5]
LexicalEnvironment
4. [setTimeout, i] [i=4]
LexicalEnvironment
3. [setTimeout, i] [i=3]
LexicalEnvironment
2. [setTimeout, i] [i=2]
LexicalEnvironment
1. [setTimeout, i] [i=1]
LexicalEnvironment
0. [setTimeout, i] [i=0]
当按时()被称为执行内容时,将创建一个内容,该内容将包含每个字符串的变量环境和所有相应的语法环境。
更简单的例子
功能范围
function test() {
for(var z = 0; z < 69; z++) {
//todo
}
//z is visible outside the loop
}
区块范围
function test() {
for(let z = 0; z < 69; z++) {
//todo
}
//z is not defined :(
}
简而言之,Let和Var之间的区别在于Var是功能分解,Let是区块分解。
让
区块范围
在顶级(功能之外)
var globalVariable = 42;
let blockScopedVariable = 43;
console.log(globalVariable); // 42
console.log(blockScopedVariable); // 43
console.log(this.globalVariable); // 42
console.log(this.blockScopedVariable); // undefined
在一个功能之内
(() => {
var functionScopedVariable = 42;
let blockScopedVariable = 43;
console.log(functionScopedVariable); // 42
console.log(blockScopedVariable); // 43
})();
console.log(functionScopedVariable); // ReferenceError: functionScopedVariable is not defined
console.log(blockScopedVariable); // ReferenceError: blockScopedVariable is not defined
在一个区块内
{
var globalVariable = 42;
let blockScopedVariable = 43;
console.log(globalVariable); // 42
console.log(blockScopedVariable); // 43
}
console.log(globalVariable); // 42
console.log(blockScopedVariable); // ReferenceError: blockScopedVariable is not defined
在一个圈子里
用Let in Loops宣言的变量只能在该圈内提到。
for (var i = 0; i < 3; i++) {
var j = i * 2;
}
console.log(i); // 3
console.log(j); // 4
for (let k = 0; k < 3; k++) {
let l = k * 2;
}
console.log(typeof k); // undefined
console.log(typeof l); // undefined
// Trying to do console.log(k) or console.log(l) here would throw a ReferenceError.
// Logs 3 thrice, not what we meant.
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
// Logs 0, 1 and 2, as expected.
for (let j = 0; j < 3; j++) {
setTimeout(() => console.log(j), 0);
}
临时死区
由于临时死亡区,使用声明的变量在被声明之前无法访问,试图这样做会导致错误。
console.log(noTDZ); // undefined
var noTDZ = 43;
console.log(hasTDZ); // ReferenceError: hasTDZ is not defined
let hasTDZ = 42;
没有重新宣布
var a;
var a; // Works fine.
let b;
let b; // SyntaxError: Identifier 'b' has already been declared
var c;
let c; // SyntaxError: Identifier 'c' has already been declared
格斯特
没有重新分配
const a = 42;
a = 43; // TypeError: Assignment to constant variable.
请注意,这并不意味着值是不可改变的;它的属性仍然可以改变。
const obj = {};
obj.a = 42;
console.log(obj.a); // 42
如果您想要一个不可变的对象,您应该使用 Object.freeze()。
const obj = Object.freeze({a: 40});
obj.a = 42;
console.log(obj.a); // 40
console.log(obj.b); // undefined
初步者需要
在使用 const 表示变量时,您必须始终指定值。
const a; // SyntaxError: Missing initializer in const declaration
如果我正确地阅读规格,那么幸运的是,也可以被利用,以避免自我呼吁的功能,用于模拟私人成员 - 一个流行的设计模式,减少代码的可读性,复杂的漏洞,没有添加真正的代码保护或其他好处 - 除了可能满足某人的欲望,以便停止使用它。
var SomeConstructor;
{
let privateScope = {};
SomeConstructor = function SomeConstructor () {
this.someProperty = "foo";
privateScope.hiddenProperty = "bar";
}
SomeConstructor.prototype.showPublic = function () {
console.log(this.someProperty); // foo
}
SomeConstructor.prototype.showPrivate = function () {
console.log(privateScope.hiddenProperty); // bar
}
}
var myInstance = new SomeConstructor();
myInstance.showPublic();
myInstance.showPrivate();
console.log(privateScope.hiddenProperty); // error
查看“私人界面模拟”
使用Let
允许的关键词将变量声明连接到它所包含的任何区块的范围(通常是 {.. } 对),换句话说,允许随意窃取任何区块的范围为其变量声明。
让变量无法在窗口对象中访问,因为它们无法在全球范围内访问。
function a(){
{ // this is the Max Scope for let variable
let x = 12;
}
console.log(x);
}
a(); // Uncaught ReferenceError: x is not defined
当使用VAR
在 ES5 中, var 和 variables 具有函数中的曲线,这意味着在函数内有效,而不是函数本身之外。
function a(){ // this is the Max Scope for var variable
{
var x = 12;
}
console.log(x);
}
a(); // 12
如果你想知道更多,继续阅读下面
最著名的面试问题之一的范围也可能足以准确使用下列;
使用Let
for (let i = 0; i < 10 ; i++) {
setTimeout(
function a() {
console.log(i); //print 0 to 9, that is literally AWW!!!
},
100 * i);
}
当使用VAR
for (var i = 0; i < 10 ; i++) {
setTimeout(
function a() {
console.log(i); //print 10 times 10
},
100 * i);
}
这是因为在使用VAR时,对于每个曲线 iteration,变量被分解并共享副本。
以下两个功能可以显示差异:
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
}