使用“let"和“let"有什么区别?和“变量"? [英] What's the difference between using "let" and "var"?

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

问题描述

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

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

有什么区别?什么时候应该使用 let 而不是 var?

What are the differences?. When should let be used instead of var?

推荐答案

范围规则

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

Scoping rules

The 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); // Foo Bar

  {
    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz
  }

  console.log(moo); // Mooo
  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.

另一个堆栈溢出问题看一下这个例子:

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]();
}

My value: 3 每次调用 funcs[j](); 时都会输出到控制台,因为匿名函数绑定到同一个变量.

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 values 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. The variable is 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();

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

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

var foo = "Foo";  // globally scoped
let bar = "Bar"; // not allowed to be globally scoped

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

在严格模式下,var 会让你在相同的范围内重新声明相同的变量,而 let 会引发 SyntaxError.

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, 'foo1' is replaced with 'foo2'.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆