天天看点

javascript var let或const您应该使用哪一个 什么是范围? (What is Scope?) Var关键字 (The Var Keyword) 让关键字 (The Let Keyword) const关键字 (The Const Keyword) 概要 (Summary)

ES6 came with a lot of great new features including two new ways to define variables in JavaScript. There are now three different keywords or identifiers to declare a variable in JavaScript. In this article, I will explain the main differences between var, let, and const and when should you use them.

ES6具有许多很棒的新功能,包括在JavaScript中定义变量的两种新方法。 现在有三个不同的关键字或标识符可在JavaScript中声明变量。 在本文中,我将解释var , let和const之间的主要区别以及何时使用它们。

In order to fully understand the differences between each of the identifiers we first need to understand the concept of scope.

为了充分理解每个标识符之间的区别,我们首先需要了解范围的概念。

什么是范围? (What is Scope?)

Scope determines the accessibility or visibility of variables to JavaScript. There are three types of scope in JavaScript:

范围确定变量对JavaScript的可访问性或可见性。 JavaScript中有三种作用域:

1. Global scope

1.全球范围

2. Function (local) scope

2.功能(本地)范围

3. Block scope (new with ES6)

3.块范围(ES6新增)

Variables declared outside a function are in the global scope. Global variables can be accessed and changed in any other scope. Variables defined within a function are in local scope and are not accessible in other functions. Each function, when invoked, creates a new scope, therefore variables with the same name can be used in different functions.

在函数外部声明的变量在全局范围内。 全局变量可以在任何其他范围内访问和更改。 在函数中定义的变量在本地范围内,并且在其他函数中不可访问。 每个函数在调用时都会创建一个新的作用域,因此具有相同名称的变量可以在不同的函数中使用。

Block scope includes if statements and loops, or any other code wrapped in {}. When invoked, they don’t create a new scope. Variables declared inside a block scope will remain in the scope they were already in.

块作用域包括if语句和循环,或包装在{}中的任何其他代码。 调用它们时,它们不会创建新的作用域。 在块作用域内声明的变量将保留在它们已经存在的作用域内。

Var关键字 (The Var Keyword)

Before ES6, the var keyword was used to declare a variable in JavaScript. The var keyword has been around since the inception of JavaScript, and it’s what you will see in any pre ES6 code.

在ES6之前,var关键字用于在JavaScript中声明变量。 自从JavaScript诞生以来,var关键字就出现了,这是您在任何ES6之前的代码中都会看到的。

Variables declared using the var keyword are either globally or functionally scoped, they do not support block-level scope. This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.

使用var关键字声明的变量是全局范围或功能范围的,它们不支持块级范围。 这意味着,如果在循环或if语句中定义了变量,则可以在块外部对其进行访问,并意外地对其进行重新定义,从而导致程序出错。 通常,应避免使用var关键字。

javascript var let或const您应该使用哪一个 什么是范围? (What is Scope?) Var关键字 (The Var Keyword) 让关键字 (The Let Keyword) const关键字 (The Const Keyword) 概要 (Summary)

让关键字 (The Let Keyword)

In many ways, the let keyword is very similar to the var keyword as they both allow you to reassign the value later on. The main difference between the two is that let deals with a block scope and although it can be reassigned it cannot be redeclared.

在许多方面,let关键字与var关键字非常相似,因为它们都允许您稍后重新分配值。 两者之间的主要区别是let处理块作用域,尽管可以重新分配它,但不能重新声明。

javascript var let或const您应该使用哪一个 什么是范围? (What is Scope?) Var关键字 (The Var Keyword) 让关键字 (The Let Keyword) const关键字 (The Const Keyword) 概要 (Summary)

const关键字 (The Const Keyword)

Like the let keyword, the const keyword is also blocked scoped. However, declaring a variable with the const keyword means that it cannot only be redeclared but also it cannot be reassigned. Therefore, every const variable must be assigned a value at the time of declaration.

与let关键字一样,const关键字也被限制作用域。 但是,使用const关键字声明变量意味着不仅不能重新声明它,而且不能重新分配它。 因此,必须在声明时为每个const变量分配一个值。

javascript var let或const您应该使用哪一个 什么是范围? (What is Scope?) Var关键字 (The Var Keyword) 让关键字 (The Let Keyword) const关键字 (The Const Keyword) 概要 (Summary)

概要 (Summary)

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let.

通常,应该始终使用const声明变量,如果您意识到需要更改变量的值,请返回并将其更改为let。

Use let when you know that the value of a variable will change.

Use const for every other variable.

Do not use var.

当您知道变量的值将更改时,请使用let。 将const用于其他所有变量。 不要使用var。

javascript var let或const您应该使用哪一个 什么是范围? (What is Scope?) Var关键字 (The Var Keyword) 让关键字 (The Let Keyword) const关键字 (The Const Keyword) 概要 (Summary)
翻译自: https://codeburst.io/javascript-var-let-or-const-which-one-should-you-use-2fd521b050fa