当前位置:网站首页>ES6 let与const
ES6 let与const
2022-07-28 08:05:00 【w͏l͏j͏】
let与const
let
- 暂时性死区
ES6 明确规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。语法上成为暂时性死区。
- typeof运算符是百分之百安全的设定不再成立。
typeof a; // ReferenceError
let a;
- 不允许重复声明变量。
- 出现块级作用域的概念。
块: { }
ES6 引入了块级作用域,明确允许在块级作用域之中声明函数。ES6 规定,块级作用域之中,函数声明语句的行为类似于let,在块级作用域之外不可引用。
- ES6 的块级作用域必须有大括号,如果没有大括号,JavaScript 引擎就认为不存在块级作用域。let只能出现在当前作用域的顶层,函数也是如此。
let flag = false;
if(flag) let b = 2; // 报错
// 正确
if (flag) {
let b = 2;
}
const
- const声明一个只读的常量。一旦声明,常量的值就不能改变。
- 块级作用域内有效。
- 存在暂时性死区,作用域不可提升。
- const实际上保证的,并不是变量的值不得改动,而是变量指向的那个内存地址所保存的数据不得改动。(特别注意)
ES6中声明变量的六种方式
var[es5],function[es5],let,const,import,class
顶层对象
- 浏览器环境指的是window,node环境指的是global。
- ES6 开始,全局变量将逐步与顶层对象的属性脱钩。
- ES2020 在语言标准的层面,引入globalThis作为顶层对象。也就是说,任何环境下,globalThis都是存在的,都可以从它拿到顶层对象,指向全局环境下的this。
边栏推荐
- PostgreSQL queries [table field type] and [all series] in the library
- Smart software completed round C financing, making Bi truly "inclusive"
- Vs2015 use dumpbin to view the exported function symbols of the library
- ES查询索引字段的分词结果
- Hyperlink label
- Hundreds of billions of it operation and maintenance market has come to the era of speaking by "effect"
- 49 opencv deep analysis profile
- JSON 文件存储
- [cloud computing] several mistakes that enterprises need to avoid after going to the cloud
- Round C financing has been completed! Smart software leads domestic Bi ecological empowerment, and products and services are a step forward
猜你喜欢

Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)

No one wants to tell the truth about kubernetes secret

Hcip day 9_ BGP experiment

DIY system home page, your personalized needs PRO system to meet!

Marketing play is changeable, and understanding the rules is the key!

1299_ Task status and switching test in FreeRTOS

Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?

Distributed system architecture theory and components

Go synergy
![Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]](/img/9c/b4ebe608cf639b8348adc1f1cc71c8.png)
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
随机推荐
49 opencv deep analysis profile
c语言数组指针和指针数组辨析,浅析内存泄漏
Hundreds of billions of it operation and maintenance market has come to the era of speaking by "effect"
In addition to exporting the incremental data captured by Oracle golden gate to Oracle, where can it be exported? Can be similar
mysql5.7.38容器里启动keepalived
CSV文件存储
看完这12个面试问题,新媒体运营岗位就是你的了
侯捷STL标准库和泛型编程
Huid learning 7: Hudi and Flink integration
Hcip day 8
Go interface Foundation
Setting of parameter configuration tool for wireless vibrating wire collector
MDM data quality application description
What content does the new version of network security level protection evaluation report template contain? Where can I find it?
我来教你如何组装一个注册中心?
[soft test software evaluator] 2013 comprehensive knowledge over the years
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture
Mobaxtermsession synchronization
Kubernetes technology and Architecture (VII)
Competition: diabetes genetic risk detection challenge (iFLYTEK)