var,const,let

2018-05-28  本文已影响0人  小菜鸟Soul
变量声明:var,const,let

在JavaScript中,有三个关键字可用于声明一个变量,并且每个关键字都有其不同之处。那些是varlet而且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
参考资料
上一篇 下一篇

猜你喜欢

热点阅读