当前位置:网站首页>ES6 learning path (II) let & const
ES6 learning path (II) let & const
2022-06-30 09:09:00 【Ancient dust left Taidao】
let
except var Now you can use let Declare variables
let The declared variable has a block level scope
{
let a = 1
var b = 2
}
console.log(a) // ReferenceError: a is not defined
console.log(b) // The above error line of code did not run , Normally, it can be printed 2 Of
Blocks do not affect each other , If the following n use var Definition , that n Namely 3 了
(() => {
let n = 5
if (true) {
let n = 3
}
console.log(n) // 5
})();
Duplicate declaration of variables... Is not allowed
let i = 0
let i = 1 // Uncaught SyntaxError: Identifier 'i' has already been declared
for Circulation and let
for Inside the loop body is a child scope
for (let i = 0; i < 2; ++i) {
let i = 'a'
console.log(i) // a It was printed 2 Time , Because the same scope variable cannot be declared repeatedly , So inside the loop i It's different
}
No variable promotion
console.log(foo) // undefined
var foo = 'a'
console.log(bar) // Uncaught ReferenceError: Cannot access 'bar' before initialization
let bar = 'b'
use let Declared variables , Can only be defined before using ,var Variables defined , The script exists when it runs . It seems js It was designed very casually , Ha ha ha . When beginners learn java When , Remember that variables must declare types , I never thought that variables could be used before they were declared .
Temporary dead zone
One sentence summary : use let and const Declared variables , Remember to define first , After use , Otherwise, it will report a mistake .
Interesting knowledge
var foo = 'a';
(() => {
console.log(foo)
if (false) {
var foo = 'b'
}
})();
The console prints undefined, My personal understanding is , although if Statement not executed , But when the script runs ,if Statement internal var foo Is running , That is to say foo This variable exists , And covered with an outer layer , It's just that there's no assignment . No assignment , Didn't the representative program go in .
miscellaneous
- Try to avoid declaring functions in a block level scope
- Block level scopes must have braces
- Remember to declare before you use
const
const It is usually used to declare constants , That is, once this variable is declared , The value cannot be changed . Objects and arrays , Except for functions , Because objects, arrays and functions belong to reference data types . For example, I define an object obj, const obj = {}, Upper figure .obj What is actually stored is a pointer , Point to obj The location of this object in the heap . So for obj Adding and deleting attributes is ok Of , But if you obj = {}, It's going to be a mistake , Because you modified pointer Pointer pointing .
const a = 1
console.log(++a) // Uncaught TypeError: Assignment to constant variable.
Same as let There is a temporary deadband , Block level scope , Variables also need to be defined first , After use .
Top objects
Code up
var a = 1
console.log(window.a) // 1
let b = 2
console.log(window.b) // undefined
In the browser environment , Top level objects refer to window, stay node The middle finger is global. The assignment of top-level objects is the same as that of global variables .
边栏推荐
- 挖财开户安全吗?怎么有人说不靠谱。
- Dart asynchronous task
- Flink Exception -- No ExecutorFactory found to execute the application
- What are the SQL add / delete / modify queries?
- mysql基础入门 day3 动力节点[老杜]课堂笔记
- 【付费推广】常见问题合集,推荐榜单FAQ
- Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- Introduction to the runner of mmcv
- Esp32 (7): I2S and I2C drivers for function development
猜你喜欢

Pytorch BERT

Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu
![[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.](/img/77/eb66ec83b34c251e732d495fbaa951.jpg)
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.

Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)

基于Svelte3.x桌面端UI组件库Svelte UI

Deep understanding of kotlin collaboration context coroutinecontext

Flink Exception -- No ExecutorFactory found to execute the application

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

Anchorgenerator for mmdet line by line interpretation

Mmcv expanding CUDA operator beginner level chapter
随机推荐
So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
Explanation on the use of password profiteering cracking tool Hydra
Detailed explanation of pytoch's scatter function
Esp32 (7): I2S and I2C drivers for function development
Self made GIF dynamic graph -gifcam
Flink sql -- No factory implements ‘org.apache.flink.table.delegation.ExecutorFactory‘.
Dart asynchronous task
Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
Maxiouassigner of mmdet line by line interpretation
Rew acoustic test (II): offline test
Mmdet line by line deltaxywhbboxcoder
Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
[data analysis and display]
Evaluation standard for audio signal quality of intelligent speakers
Raspberry pie 4B no screen installation system and networking using VNC wireless projection function
Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
Flink 数据偶尔数据积压导致checkpoint失败
127.0.0.1、0.0.0.0和localhost
Harmonyos actual combat - ten thousand words long article understanding service card development process