变量声明:var,const,let
在JavaScript中,有三个关键字可用于声明一个变量,并且每个关键字都有其不同之处。那些是var
,let
而且const
简短的解释
使用const
关键字声明的变量不能被重新赋值,let
而且var
可以。
范围 | 重新赋值 | 易变的 | 暂时性死区 | |
---|---|---|---|---|
const | 块 | 否 | 是 | 是 |
let | 块 | 是 | 是 | 是 |
var | 功能 | 是 | 是 | 否 |
示例代码
const person = "Nick";
person = "John"; // Will raise an error, person can't be reassigned
let person = "Nick";
person = "John";
console.log(person); // "John", reassignment is allowed with let
详细的解释
变量的范围大致意味着“代码中可用的变量的作用域”。
var
var
声明的变量是函数作用域的,这意味着当在函数中创建变量时,该函数中的所有内容都可以访问该变量。此外,函数中创建的函数作用域变量不能在此函数之外访问。
示例代码
function myFunction() {
var myVar = "Nick";
console.log(myVar); // "Nick" - myVar is accessible inside the function
}
console.log(myVar); // Throws a ReferenceError, myVar is not accessible outside the function.
function myFunction() {
var myVar = "Nick";
if (true) {
var myVar = "John";
console.log(myVar); // "John"
// actually, myVar being function scoped, we just erased the previous myVar value "Nick" for "John"
}
console.log(myVar); // "John" - see how the instructions in the if block affected this value
}
console.log(myVar); // Throws a ReferenceError, myVar is not accessible outside the function.
这部分代码:
console.log(myVar) // undefined -- no error raised
var myVar = 2;
在执行中被理解为:
var myVar;
console.log(myVar) // undefined -- no error raised
myVar = 2;
let
var和let大致相同,但let声明的变量
- 是块范围
- 不存在变量提升
- 不能在同一范围内重新声明
示例代码
function myFunction() {
let myVar = "Nick";
if (true) {
let myVar = "John";
console.log(myVar); // "John"
// actually, myVar being block scoped, we just created a new variable myVar.
// this variable is not accessible outside this block and totally independent
// from the first myVar created !
}
console.log(myVar); // "Nick", see how the instructions in the if block DID NOT affect this value
}
console.log(myVar); // Throws a ReferenceError, myVar is not accessible outside the function.
现在,let(和const)变量在分配前不可访问的含义是什么:
// var 的情况
console.log(foo); // 输出undefined
var foo = 2;
// let 的情况
console.log(bar); // 报错ReferenceError
let bar = 2;
与var变量相比,如果在分配之前尝试读取或写入let或const变量,则会引发错误。这种现象通常称为暂时性死区或TDZ。
另外,你不能重新声明一个let变量:
let myVar = 2;
let myVar = 3; // Raises a SyntaxError
const (常量)
const声明的变量行为就像let变量一样,但不能被重新赋值。
总结一下,const变量:
- 是块范围
- 在赋值之前不可访问
- 不能在同一范围内重新定义
- 不能重新赋值
实例代码
const myVar = "Nick";
myVar = "John" // raises an error, reassignment is not allowed
const myVar = "Nick";
const myVar = "John" // raises an error, re-declaration is not allowed
但有一个微妙之处:const变量不是不变的!具体而言,这意味着对象和数组 const声明的变量可能会发生变化。
对于对象:
const person = {
name: 'Nick'
};
person.name = 'John' // this will work ! person variable is not completely reassigned, but mutated
console.log(person.name) // "John"
person = "Sandra" // raises an error, because reassignment is not allowed with const declared variables
对于数组:
const person = [];
person.push('John'); // this will work ! person variable is not completely reassigned, but mutated
console.log(person[0]) // "John"
person = ["Nick"] // raises an error, because reassignment is not allowed with const declared variables