当前位置:网站首页>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 .
边栏推荐
- Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
- Raspberry pie 4B no screen installation system and networking using VNC wireless projection function
- What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
- Esp32 (6): Bluetooth and WiFi functions for function development
- CUDA realizes matrix multiplication
- Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
- Summary of common pytoch APIs
- Interpretation of source code demand:a rotation equivariant detector for aerial object detection
- Mmdet line by line code interpretation of positive and negative sample sampler
- Comparaison de deux façons d'accéder à la base de données SQL Server (sqldatareader vs sqldataadapter)
猜你喜欢

Pytorch BERT

Detailed explanation of pytoch's scatter function

Maxiouassigner of mmdet line by line interpretation

Raspberry pie 4B no screen installation system and networking using VNC wireless projection function

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!

Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"

Installation, use and explanation of vulnerability scanning tool OpenVAS

Detailed explanation of pipline of mmdetection

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

Rew acoustic test (I): microphone calibration
随机推荐
C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
Anchorgenerator for mmdet line by line interpretation
Introduction to MySQL basics day4 power node [Lao Du] class notes
C accesses mongodb and performs CRUD operations
Rew acoustic test (I): microphone calibration
Why must redis exist in distributed systems?
Detailed explanation of pytoch's scatter function
List set export excel table
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())
Flutter theme (skin) changes
127.0.0.1, 0.0.0.0 and localhost
Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
证券开户的优惠怎样才能得到?在线开户安全?
启动jar包报错UnsupportedClassVersionError,如何修复
Based on svelte3 X desktop UI component library svelte UI
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
Viteproject require Syntax Compatibility Problem Solving require is not defined
asdsadadsad
Flink sql -- No factory implements ‘org. apache. flink. table. delegation. ExecutorFactory‘.
About Lombok's @data annotation