var、let、const三种声明变量方式之间的差别

var、let、const三种声明变量方式之间的差别
  • var声明的变量会挂载到window上,而let和const声明的变量不会
var a = 'foo'
console.log(window.a) // foo

let b = 'bar'
console.log(window.b) // undefined

const c = 'baz'
console.log(window.c) // undefined复制代码
  • var 存在变量提高,而let和const不存在变量提高,let和const上面的区域称为“暂时性死区”,在定义以前不能够使用
console.log(a) // undefined
var a = 'foo'

console.log(b) // test4.html:14 Uncaught ReferenceError: Cannot access 'b' before initialization
let b = 'bar'

console.log(c) // test4.html:14 Uncaught ReferenceError: Cannot access 'c' before initialization
const c = 'baz'复制代码
  • let和const的声明会造成块级做用域
if (true) {
    var a = 'foo'
    let b = 'bar'
}
console.log(a) // foo
console.log(b) // Uncaught ReferenceError: b is not defined

if (true) {
    var a = 'foo'
    const c = 'baz'
}
console.log(a) // foo
console.log(c) // Uncaught ReferenceError: c is not defined复制代码
  • 在同一个做用域下,let和const不能再次声明同一个变量,而var能够
var a = 'foo'
var a = 'bar'
console.log(a) // bar

let b = 'foo'
let b = 'bar'
console.log(b) // test4.html:34 Uncaught SyntaxError: Identifier 'b' has already been declared

const c = 'foo'
const c = 'bar'
console.log(c) // Uncaught SyntaxError: Identifier 'c' has already been declared复制代码
  • let定义变量以后能够修改,而const表面上像是声明一个“常量”。const并非保证变量的值不得改动,而是指变量指向的内存地址不得改变。对于简单数据类型(Number、String、Boolean),值就保存在变量指向的内存地址,所以等同于常量;而对于复合数据类型(主要是对象和数组),变量只是保存了指向堆内存的地址,至于堆内的数据是否是可变的,就不能控制了。
const person = {}
person.name = 'maoxiaoxing'
console.log(person) // {name: "maoxiaoxing"}

person = {name: 'king'} // test4.html:45 Uncaught TypeError: Assignment to constant variable.

const a = ['foo']
a.push('bar')
a[2] = 'baz'
console.log(a) // ["foo", "bar", "baz"]复制代码
相关文章
相关标签/搜索