当前位置:网站首页>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
边栏推荐
- Redis的基础使用
- CSDN markdown editor
- vs2019 第一个MFC应用程序
- 项目实战-后台员工信息管理(增删改查登录与退出)
- 天梯赛练习集题解LV1(all)
- [recommended by bloggers] background management system of SSM framework (with source code)
- 基于apache-jena的知识问答
- Kept VRRP script, preemptive delay, VIP unicast details
- Swagger, Yapi interface management service_ SE
- AcWing 1294. Cherry Blossom explanation
猜你喜欢
Solve the problem of installing failed building wheel for pilot
Windows下安装MongDB教程、Redis教程
打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
AcWing 242. A simple integer problem (tree array + difference)
基于apache-jena的知识问答
Install mysql5.5 and mysql8.0 under windows at the same time
QT creator specify editor settings
QT creator support platform
[number theory] divisor
Copie maître - esclave MySQL, séparation lecture - écriture
随机推荐
Learn winpwn (3) -- sEH from scratch
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
AcWing 179.阶乘分解 题解
ES6 let 和 const 命令
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
Armv8-a programming guide MMU (2)
Introduction to the easy copy module
牛客Novice月赛40
[蓝桥杯2017初赛]方格分割
02-项目实战之后台员工信息管理
Cookie setting three-day secret free login (run tutorial)
Software I2C based on Hal Library
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
Why can't STM32 download the program
Use dapr to shorten software development cycle and improve production efficiency
使用lambda在循环中传参时,参数总为同一个值
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
Knowledge Q & A based on Apache Jena
AcWing 1298.曹冲养猪 题解
机器学习--人口普查数据分析