当前位置:网站首页>ES6 let and const commands
ES6 let and const commands
2022-07-06 11:24:00 【imxlw00】
let Basic usage
let command , Used to declare variables . Its usage is similar to var
let a = 10;
var b = 1;
No variable promotion
var The command will happen “ Variable Promotion ” The phenomenon , That is, variables can be used before they are declared , The value is undefined
// var The situation of
console.log(foo); // Output undefined
var foo = 2;
// let The situation of
console.log(bar); // Report errors ReferenceError
let bar = 2;
Temporary dead zone
ES6 Make it clear , If a block exists let and const command , Variables declared by this block for these commands , Closed scopes have been formed from the beginning . Always use these variables before declaring them , You're going to report a mistake .
var tmp = 123;
if (true) {
tmp = 'abc'; // ReferenceError
let tmp;
}
In the above code , There are global variables tmp, But in block level scope let A local variable is declared tmp, Causes the latter to bind the block level scope , So in let Before declaring variables , Yes tmp The assignment will report an error .
Duplicate statements are not allowed
// Report errors
function func() {
let a = 10;
var a = 1;
}
// Report errors
function func() {
let a = 10;
let a = 1;
}
Block level scope
{
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
}
The function above has two blocks of code , Variables are declared n, Post run output 5. This means that the outer code block is not affected by the inner code block . If you use it both times var Defining variables n, The final output value is 10.
const Command basic usage
const Declare a read-only constant . Once declared , You can't change the value of a constant .
<script type="text/javascript">
const PI = 3.1415;
console.log(PI); // 3.1415
PI = 3;
// TypeError: Assignment to constant variable.
</script>
const The scope of let The same command : Valid only in the block level scope where the declaration is located .
const The constant declared by the command is also not promoted , There is also a temporary dead zone , Can only be used after the declared position .
const Command essence
const Actually guaranteed , It's not that the value of a variable can't be changed , Instead, the memory address that the variable points to holds the same data .
const foo = {
};
// by foo Add an attribute , Can succeed
foo.prop = 123;
foo.prop // 123
// take foo Point to another object , You're going to report a mistake
foo = {
}; // TypeError: "foo" is read-only
const a = [];
a.push('Hello'); // Executable
a.length = 0; // Executable
a = ['Dave']; // Report errors
Properties of the top-level object
In browser environment, it means window object
window.a = 1;
a // 1
a = 2;
window.a // 2
ES6 To change that , On the one hand, it stipulates that , To maintain compatibility ,var Command and function Global variables declared by the command , It is still the property of the top-level object ; On the other hand, it stipulates that ,let command 、const command 、class Global variables declared by the command , Properties that do not belong to the top-level object . in other words , from ES6 Start , Global variables will gradually decouple from the properties of the top-level object .
var a = 1;
// If in Node Of REPL Environmental Science , It can be written. global.a
// Or in a general way , It's written in this.a
window.a // 1
let b = 1;
window.b // undefined
In the above code , Global variables a from var A statement of order , So it's a property of the top-level object ; Global variables b from let A statement of order , So it's not a property of the top-level object , return undefined.
Reference link :https://es6.ruanyifeng.com/#docs/let
边栏推荐
- Django运行报错:Error loading MySQLdb module解决方法
- 01 project demand analysis (ordering system)
- [蓝桥杯2017初赛]包子凑数
- Number game
- Ubuntu 20.04 安装 MySQL
- Tcp/ip protocol (UDP)
- AcWing 1298. Solution to Cao Chong's pig raising problem
- Codeforces Round #753 (Div. 3)
- Use dapr to shorten software development cycle and improve production efficiency
- 软件测试与质量学习笔记3--白盒测试
猜你喜欢
MySQL主从复制、读写分离
QT creator design user interface
Why can't I use the @test annotation after introducing JUnit
Classes in C #
QT creator specifies dependencies
Request object and response object analysis
C语言读取BMP文件
AcWing 1298.曹冲养猪 题解
One click extraction of tables in PDF
Django running error: error loading mysqldb module solution
随机推荐
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
Some notes of MySQL
JDBC原理
安全测试涉及的测试对象
引入了junit为什么还是用不了@Test注解
Leetcode 461 Hamming distance
vs2019 桌面程序快速入门
Rhcsa certification exam exercise (configured on the first host)
C语言读取BMP文件
Windows下安装MongDB教程、Redis教程
学习问题1:127.0.0.1拒绝了我们的访问
Request object and response object analysis
数数字游戏
How to configure flymcu (STM32 serial port download software) is shown in super detail
数据库高级学习笔记--SQL语句
Basic use of redis
QT creator test
Learn winpwn (2) -- GS protection from scratch
Armv8-a programming guide MMU (2)
MySQL completely uninstalled (windows, MAC, Linux)