当前位置:网站首页>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 .
边栏推荐
- Flink SQL custom connector
- File upload component on success event, add custom parameters
- Maxiouassigner of mmdet line by line interpretation
- JVM调优相关命令以及解释
- Design specification for smart speakers v1.0
- Understanding society at the age of 14 - reading notes on "happiness at work"
- Self made GIF dynamic graph -gifcam
- Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
- El input limit can only input numbers
- Flutter theme (skin) changes
猜你喜欢
随机推荐
[paid promotion] collection of frequently asked questions, FAQ of recommended list
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
基于Svelte3.x桌面端UI组件库Svelte UI
The elegant combination of walle and Jianbao
Summary of common pytoch APIs
C # get the current timestamp
Source code interpretation of detectron2 1--engine
JVM tuning related commands and explanations
Installation, use and explanation of vulnerability scanning tool OpenVAS
127.0.0.1, 0.0.0.0 and localhost
Invalid update: invalid number of sections. The number of sections contained in the table view after
Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development
Dart asynchronous task
酒精测试仪方案:酒精测试仪是根据什么原理测酒精溶度?
Evaluation standard for audio signal quality of intelligent speakers
Flink SQL custom connector
Concatapter tutorial
Is the reverse repurchase of treasury bonds absolutely safe? How to open an account online
Icon resources
Flutter theme (skin) changes









![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)