当前位置:网站首页>ES6 let 和 const 命令
ES6 let 和 const 命令
2022-07-06 09:14:00 【imxlw00】
let 基本用法
let命令,用来声明变量。它的用法类似于var
let a = 10;
var b = 1;
不存在变量提升
var命令会发生“变量提升”现象,即变量可以在声明之前使用,值为undefined
// var 的情况
console.log(foo); // 输出undefined
var foo = 2;
// let 的情况
console.log(bar); // 报错ReferenceError
let bar = 2;
暂时性死区
ES6 明确规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。
var tmp = 123;
if (true) {
tmp = 'abc'; // ReferenceError
let tmp;
}
上面代码中,存在全局变量tmp,但是块级作用域内let又声明了一个局部变量tmp,导致后者绑定这个块级作用域,所以在let声明变量前,对tmp赋值会报错。
不允许重复声明
// 报错
function func() {
let a = 10;
var a = 1;
}
// 报错
function func() {
let a = 10;
let a = 1;
}
块级作用域
{
let a = 10;
var b = 1;
}
a // ReferenceError: a is not defined.
b // 1
function f1() {
let n = 5;
if (true) {
let n = 10;
}
console.log(n); // 5
}
上面的函数有两个代码块,都声明了变量n,运行后输出 5。这表示外层代码块不受内层代码块的影响。如果两次都使用var定义变量n,最后输出的值才是 10。
const命令基本用法
const声明一个只读的常量。一旦声明,常量的值就不能改变。
<script type="text/javascript">
const PI = 3.1415;
console.log(PI); // 3.1415
PI = 3;
// TypeError: Assignment to constant variable.
</script>
const的作用域与let命令相同:只在声明所在的块级作用域内有效。
const命令声明的常量也是不提升,同样存在暂时性死区,只能在声明的位置后面使用。
const命令本质
const实际上保证的,并不是变量的值不得改动,而是变量指向的那个内存地址所保存的数据不得改动。
const foo = {
};
// 为 foo 添加一个属性,可以成功
foo.prop = 123;
foo.prop // 123
// 将 foo 指向另一个对象,就会报错
foo = {
}; // TypeError: "foo" is read-only
const a = [];
a.push('Hello'); // 可执行
a.length = 0; // 可执行
a = ['Dave']; // 报错
顶层对象的属性
在浏览器环境指的是window对象
window.a = 1;
a // 1
a = 2;
window.a // 2
ES6 为了改变这一点,一方面规定,为了保持兼容性,var命令和function命令声明的全局变量,依旧是顶层对象的属性;另一方面规定,let命令、const命令、class命令声明的全局变量,不属于顶层对象的属性。也就是说,从 ES6 开始,全局变量将逐步与顶层对象的属性脱钩。
var a = 1;
// 如果在 Node 的 REPL 环境,可以写成 global.a
// 或者采用通用方法,写成 this.a
window.a // 1
let b = 1;
window.b // undefined
上面代码中,全局变量a由var命令声明,所以它是顶层对象的属性;全局变量b由let命令声明,所以它不是顶层对象的属性,返回undefined。
边栏推荐
- Project practice - background employee information management (add, delete, modify, check, login and exit)
- Some notes of MySQL
- Test objects involved in safety test
- Ansible实战系列一 _ 入门
- MySQL的一些随笔记录
- Introduction and use of automatic machine learning framework (flaml, H2O)
- Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- Asp access Shaoxing tourism graduation design website
- MySQL主从复制、读写分离
- Windows下安装MongDB教程、Redis教程
猜你喜欢
机器学习--人口普查数据分析
Some problems in the development of unity3d upgraded 2020 VR
Request object and response object analysis
一键提取pdf中的表格
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
[recommended by bloggers] C WinForm regularly sends email (with source code)
Idea import / export settings file
QT creator specify editor settings
MySQL主從複制、讀寫分離
Swagger、Yapi接口管理服务_SE
随机推荐
MySQL master-slave replication, read-write separation
Attention apply personal understanding to images
Data dictionary in C #
Learn winpwn (3) -- sEH from scratch
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Codeforces Round #771 (Div. 2)
[AGC009D]Uninity
Pytorch基础
Ansible practical Series III_ Task common commands
Learn winpwn (2) -- GS protection from scratch
机器学习--人口普查数据分析
QT creator support platform
Idea import / export settings file
AcWing 1298.曹冲养猪 题解
Principes JDBC
误删Path变量解决
Django运行报错:Error loading MySQLdb module解决方法
01项目需求分析 (点餐系统)
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
MySQL主從複制、讀寫分離