ECMAScript 6 引入了许可声明。

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

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


当前回答

主要差异是范围差异,而让它只能在所宣布的范围内可用,如在圆圈中,可在圆圈之外可访问,例如从MDN的文档(MDN的例子):

function varTest() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}`

var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined

当在区块内使用时,请将变量的范围限制在该区块上。

var a = 1;
var b = 2;

if (a === 1) {
  var a = 11; // the scope is global
  let b = 22; // the scope is inside the if-block

  console.log(a);  // 11
  console.log(b);  // 22
} 

console.log(a); // 11
console.log(b); // 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;当我们试图改变时,我们没有任何错误。

一些黑客与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)

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

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

主要差异是范围差异,而让它只能在所宣布的范围内可用,如在圆圈中,可在圆圈之外可访问,例如从MDN的文档(MDN的例子):

function varTest() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}`

var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined

当在区块内使用时,请将变量的范围限制在该区块上。

var a = 1;
var b = 2;

if (a === 1) {
  var a = 11; // the scope is global
  let b = 22; // the scope is inside the if-block

  console.log(a);  // 11
  console.log(b);  // 22
} 

console.log(a); // 11
console.log(b); // 2

这篇文章清楚地定义了 var, let 和 const 的区别。

const 是一个信号,识别器不会被重新分配. 让我们,这是一个信号,变量可以被重新分配, 例如,一个字符在一个旋转, 或一个值交换在一个算法. 它也信号,变量将只在区块它是定义的, 这并不总是整个内容函数. var 现在是最弱的信号可用,当你定义一个变量在 JavaScript。

https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75#.esmkpbg9b