變量聲明:var越平,const,let
在JavaScript中灵迫,有三個關(guān)鍵字可用于聲明一個變量秦叛,并且每個關(guān)鍵字都有其不同之處。那些是var
瀑粥,let
而且const
簡短的解釋
使用const
關(guān)鍵字聲明的變量不能被重新賦值挣跋,let
而且var
可以。
范圍 | 重新賦值 | 易變的 | 暫時性死區(qū) | |
---|---|---|---|---|
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
聲明的變量是函數(shù)作用域的浆劲,這意味著當(dāng)在函數(shù)中創(chuàng)建變量時,該函數(shù)中的所有內(nèi)容都可以訪問該變量哀澈。此外牌借,函數(shù)中創(chuàng)建的函數(shù)作用域變量不能在此函數(shù)之外訪問。
示例代碼
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;
在執(zhí)行中被理解為:
var myVar;
console.log(myVar) // undefined -- no error raised
myVar = 2;
let
var和let大致相同割按,但let聲明的變量
- 是塊范圍
- 不存在變量提升
- 不能在同一范圍內(nèi)重新聲明
示例代碼
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.
現(xiàn)在膨报,let(和const)變量在分配前不可訪問的含義是什么:
// var 的情況
console.log(foo); // 輸出undefined
var foo = 2;
// let 的情況
console.log(bar); // 報錯ReferenceError
let bar = 2;
與var變量相比,如果在分配之前嘗試讀取或?qū)懭雔et或const變量适荣,則會引發(fā)錯誤现柠。這種現(xiàn)象通常稱為暫時性死區(qū)或TDZ。
另外弛矛,你不能重新聲明一個let變量:
let myVar = 2;
let myVar = 3; // Raises a SyntaxError
const (常量)
const聲明的變量行為就像let變量一樣够吩,但不能被重新賦值。
總結(jié)一下丈氓,const變量:
- 是塊范圍
- 在賦值之前不可訪問
- 不能在同一范圍內(nèi)重新定義
- 不能重新賦值
實例代碼
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變量不是不變的周循!具體而言,這意味著對象和數(shù)組 const聲明的變量可能會發(fā)生變化万俗。
對于對象:
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
對于數(shù)組:
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