美文网首页
var,const,let

var,const,let

作者: 小菜鸟Soul | 来源:发表于2018-05-28 11:48 被阅读0次
变量声明: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
参考资料

相关文章

  • Android菜鸟学习js笔记 一

    一、var,const,let区别? var 变量,const 常量,let块级变量。

  • ES6_var、let与const

    var,let 与 const var 申明变量。 let 申明变量,更完美的 var。 const 申明常量(物...

  • TypeScript 02 - 变量声明

    在 TypeScript 中,var 还是 var,let 还是 let,const 还是 const,这里简单温...

  • JS中var、let、const区别

    在javascript中有三种声明变量的方式:var let const。let var const的区别?var...

  • js中const,var,let区别

    在javascript中有三种声明变量的方式:var let const。let var const的区别?var...

  • 面试题 var let const的区别 箭头函数和普通函数的区

    var let const 有什么区别 var var声明的变量可进行变量提升,let和const不会var可以重...

  • ES6学习

    1、 var let const var是函数作用域let和const是块级作用域,!const是常量,不能改变。...

  • ES6

    let var const var 可声明前置 let不可声明前置 let不可重复声明 存在块级作用域 const...

  • let和const

    let/const(常用) let和var 区别 let,const用于声明变量,用来替代老语法的var关键字,与...

  • js 2022经典面试题汇总

    1.JS数据类型有哪些 2.var、let、const区别 var存在变量提升,而let、const没有 let、...

网友评论

      本文标题:var,const,let

      本文链接:https://www.haomeiwen.com/subject/kbowjftx.html