ECMAScript 6 引入了许可声明。

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

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


当前回答

下面是让关键词的解释,有几个例子。

主要区别在于,一个变量的范围是整个关闭功能。

此图在维基百科上显示哪些浏览器支持JavaScript 1.7.

请注意,只有 Mozilla 和 Chrome 浏览器支持它. IE、Safari 和其他可能不支持它。

其他回答

如果我正确地阅读规格,那么幸运的是,也可以被利用,以避免自我呼吁的功能,用于模拟私人成员 - 一个流行的设计模式,减少代码的可读性,复杂的漏洞,没有添加真正的代码保护或其他好处 - 除了可能满足某人的欲望,以便停止使用它。

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

查看“私人界面模拟”

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

这不会导致编译错误:

var x = 1;
var x = 2;

这将是:

let x = 1;
let x = 2;

区块范围

在顶级(功能之外)

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

一些黑客与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)
var   --> Function scope  
let   --> Block scope
const --> Block scope

是的

在此样本中,你可以看到我被宣布在一个如果区块. 但它被宣布使用 var. 因此,它获得功能范围. 它意味着你仍然可以访问变量 i 内部函数 x. 因为 var 总是被推到函数. 尽管变量 i 被宣布在一个如果区块, 因为它使用 var 它被推到主函数 x。

函数 x(){ if(true){ var i = 100; } console.log(i); } x();

现在变量 i 被宣布在函数 y. 因此, i 被定义为函数 y. 您可以访问 i 内部函数 y. 但不是从外部函数 y。

let, const

让和 const 有区块范围。

但是,差异是,当你将值分配给 const 时,你不能再分配。

console.log(x); var x = 100;

console.log(x); // ERROR let x = 100;