本文介绍了使用"let"和"let"有什么区别?和"var"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript 6引入了 let语句.

ECMAScript 6 introduced the let statement.

我听说它被描述为局部"变量,但是我仍然不太确定它的行为与var关键字有何不同.

I've heard it that it's described as a "local" variable, but I'm still not quite sure how it behaves differently than the var keyword.

有什么区别?何时应在var上使用let?

What are the differences? When should let be used over var?

推荐答案

作用域规则

主要区别在于范围规则. var关键字声明的变量的作用域为直接函数主体(因此作用域),而let变量的作用域为由{ }表示的直接 enclosing 块(因此,块作用域)

Scoping rules

Main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar);

  {
    let baz = "Bazz";
    console.log(baz);
  }

  console.log(baz); // ReferenceError
}

run();

let关键字引入语言的原因是函数范围令人困惑,并且是JavaScript中错误的主要来源之一.

The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.

另一个stackoverflow问题中查看此示例:

var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
  // and store them in funcs
  funcs[i] = function() {
    // each should log its value.
    console.log("My value: " + i);
  };
}
for (var j = 0; j < 3; j++) {
  // and now let's run each one to see
  funcs[j]();
}

每次调用funcs[j]();时,都会将

My value: 3输出到控制台,因为匿名函数已绑定到同一变量.

My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable.

人们必须创建立即调用的函数才能从循环中捕获正确的值,但这也很麻烦.

People had to create immediately invoked functions to capture correct value from the loops but that was also hairy.

使用var关键字声明的变量被悬挂(在代码运行前用undefined初始化),这意味着即使在声明它们之前,也可以在其范围内对其进行访问:

While variables declared with var keyword are hoisted (initialized with undefined before the code is run) which means they are accessible in their enclosing scope even before they are declared:

function run() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

run();

let变量在评估其定义之前不会初始化.在初始化之前访问它们会导致ReferenceError.从块的开始到初始化处理之前,该变量都处于临时死区".

let variables are not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. Variable said to be in "temporal dead zone" from the start of the block until the initialization is processed.

function checkHoisting() {
  console.log(foo); // ReferenceError
  let foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

创建全局对象属性

在顶层,与var不同,let不会在全局对象上创建属性:

Creating global object property

At the top level, let, unlike var, does not create a property on the global object:

var foo = "Foo";  // globally scoped
let bar = "Bar"; // globally scoped

console.log(window.foo); // Foo
console.log(window.bar); // undefined

重新声明

在严格模式下,var将允许您在同一范围内重新声明同一变量,而let会引发SyntaxError.

Redeclaration

In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.

'use strict';
var foo = "foo1";
var foo = "foo2"; // No problem, 'foo' is replaced.

let bar = "bar1";
let bar = "bar2"; // SyntaxError: Identifier 'bar' has already been declared

这篇关于使用"let"和"let"有什么区别?和"var"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:08