One’s success depends not only on one’s own efforts, but also on the course of history.
18 years?
Look at the first edition of ECMA: 1997
Take a look at the version that supports LET: 2015
It is hard to imagine that only a language feature can take 18 years. Therefore, a person’s success not only depends on his own efforts, but also takes into account the historical process, which is also applicable to a language. A lot of people have made fun of the many flaws of JS, and I don’t think Brandon Ek can be blamed for that, because in the beginning, JS was not supposed to do anything big. It was just a simple scripting language that could implement some simple functions. It was only with the growing prosperity of the Internet, and the importance of the Web, that JavScript was put in charge, which required JavaScript to become more formal and more of a real programming language.
From the var
Statement of pre –
Var is used to declare a variable, but you need to notice some strange features of this variable declaration, which are the declaration prefixes.
In the above code, the first line uses the variable A, in C language, has not declared a variable to use, is a direct error, but in JS does not, but will output undefined this is the declaration of the front. The above code is equivalent to:
scope
Before ES6, you should know that there were only global scopes and function scopes.
If we declare a variable inside a function, we will get an error if we access it outside the function:
How about writing block-level scopes like c:
Summary:var
Bugs, predeclarations, scoping issues, not like a programming language should have variable declarations.
Let the magic
1. Temporary dead zones
Use first, then define error.
Does this mean that variables declared by let are not preloaded? Many people think that let will not be promoted, but this is not the case. Here is another example:
Temporary dead zone
let
2. Block level scope
Unlike var, let declared variables have their own independent scope.
So,let
Declared variables are valid only in their own code block, i.elet
Declared variables are local variables.
3. Repeat the statement
Repeated declaration:
Repeat the declaration in a block:
Declare in both blocks:
Reassign:
So, when in the same scope, uselet
Duplicate declarations are an error, but can be reassigned.
4. Not a window property
Var declares global variables:
Global variables declared by let:
Const declares a quantity
Const This is also a new syntax in ES6 for declaring a constant whose value cannot be modified.
Declare a constant
When using const, you must also specify the value of this constant; otherwise, an error will be reported.
Scope of const
Constants declared by const are also block-level scoped, just like lets.
The value of const cannot be modified
Const declaration creates a read-only reference to a value. This does not mean that the values it holds are immutable, just that variable identifiers cannot be reassigned.
- For simple data types, the value is stored at the memory address the variable points to.
- For composite data, the memory address to which the variable refers cannot be modified, whereas the actual data to which the variable refers can be modified.
conclusion
Let summary
let
Declared variables have temporary dead zones, the creation process is promoted, and the initialization process is not promoted.let
Declared variables have block-level scopelet
Declared variables cannot be declared twicelet
Declared variables do not act aswindow
The properties of the
Const summary
- A value must be initialized when declared
- Constant values cannot be changed
- And the other
let
Features the same
JS the development of this language is to comply with the development of history, but JS developers, will feel tired, because, in the old version also need to continue to maintain, can not give up decisively, I want to say, predecessors plant trees, future generations care.