cool hit counter ES6 Introduction to let & const_Intefrankly

ES6 Introduction to let & const


Install a plugin in vscode called code runner to run js code from within the IDE

After installation, select the code with the mouse and right click there is a Run Code, select the code can be executed

----------------------------------------- divider line----------------------------------------

Until the advent of ES6 we have always declared variables with var

var a=1; var b={} we have been using var very smoothly before, in fact, unknowingly var has a lot of unreasonable mechanism, there are some problems we do not often encounter, basically does not cause trouble to production, some are making people in the logic of the very misunderstanding

1. Statement in advance

The first situation

We directly console.log a variable that doesn't exist, and the browser will report an error, which is normal of course, after all a doesn't exist

Second scenario

We directly output a, and then in the definition of a, will show undefined, although the meaning is undefined, but the two are very different situations, according to our logic, js code is running from birth, in the run to the console.log when a are still not there, both should report an error, but the result is not the same

The reason this problem arises is because Statement in advance(It can also be a pre-explanation) In the beginning of the code, the browser will consider the code first, and all the vars and functions in the code will be extracted from the overall total and declared at the top of the code (only declared, not copied, and if it is a function, the method body will be saved as a string), which is not logical

let would have avoided this situation

variables declared by let will not be declared in advance

2.Conflict between asynchronous functions and concurrent functions

Looking at a line of code like this, any front-end person with a bit of js experience will know that the final output is

Ten tens.

We all know that for loops are executed synchronously, automatically executing the loop 10 times from the top down

And setTimeout is asynchronous, console.log will be executed only after 1s, and this time the for loop will not wait for 1s before the next loop, but loop 10 times first, and the value of i will be 10 by this time, so it will output 10 10s

And let would then have a stop-like effect, or the effect of holding the value in place

The final output results in 0 to 9, which we can call a block-level scope

It is also relatively easy to understand about const, which is the declaration of a constant, which is actually always available in object-oriented languages, such as Java. Declaring a variable that is immutable is a constant

We see that variables declared with const will report an error if they are modified

But there is one caveat to this, objects declared with const have internal variables that can be modified

If you have a deep understanding of reference type data, you can know that reference type is stored in the heap memory space, we declare my variable to store not real data, but a pointer to this heap memory space, and this pointer is a value type, after the declaration with const is not changeable, but changing the data in the heap memory space does not change the pointer data in my, so there is no conflict with const


Recommended>>
1、Electric cars expected to be less expensive than fuel cars by 2025
2、Autonomous Drivings Most Powerful Weapon Explained by Velodynes 128Line LIDAR
3、Guangdong drag chess Huize chess Bao win chess it turns out that there are hangers that control good cards
4、360 pushes block cat campaign Japanese lawmakers say government is considering possibility of central bank issuing digital currency
5、Happy five cheat software download Happy five cheat software has no cheat hangers

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号