The emergence of a new technology is certainly to solve some problems, then the emergence of ES6 is mainly to solve the problem? What convenience does it bring to us? When it does not appear, how to deal with some problems? What is the difference between the ES6 method and the previous method? Based on these questions, I started the ES6 learning journey.
ES6 is based on the ES5 of the syntax has been modified and added some new features, specifically to fix what syntax and what new features added, then start learning it.
Let
ES6 adds a let command for a variable declaration, similar to the use of Var, except that the declared variable is only valid within the block of code where the Let command resides.
{ = ten; = 1;} A // referenceerrorb // 1
View Code
The following code outputs 10 because I is VAR declared and is valid at global scope. So each cycle, the new I value will overwrite the old value, resulting in the final output of the last round of the I value. If I is a let declaration, the variable is valid only at the block-level scope, and the last output is 6.
var a = []; for (var i = 0; i <; i++function () {console.log (i);};} a[// ten
View Code
We all know that the variables in the ES5 will be lifted to the top, so we can call them first and then declare them because they can be declared by Var, and the variables that let declare are not promoted, so they must be declared before they can be called . Also, as long as the Let command exists within the block scope, the variable it declares is "bound" to the area and is no longer affected by the external . For example, the following code:
var tmp = 123; if (true// referenceerrorlettmp;}
View Code
Using let and declaring the variable tmp within a block-level scope, the variable is no longer affected by the external variable, and the let declared variable cannot be lifted, so the call will go wrong without declaring it.
In summary, in a code block, the variable is not available until the Let declaration variable is used, which is called a "transient dead zone" in ES6 syntax, and theadvent of a "temporary dead zone" means that typeof is no longer a fully secure operation.
typeof // Referenceerror Let x; typeof // "undefined"
View Code
Before declaring X, using TypeOf to detect the type of x will be an error, while detecting an undeclared variable will not cause an error.
let also does not allow repeated declarations of the same variable within the same scope, with the following error:
// error function= ten; var a = 1;} // error function= ten= 1;}
View Code
Therefore, the re-declaration of variables within the function will also error:
function// error }
View Code
It is possible to re-declare variables in different scopes:
function// no error }}
View Code
Const
Const declares a read-only constant, and once declared, the value of the constant cannot be changed because it cannot be changed, so it is necessary to initialize the variable immediately when declaring the variable, and cannot be left to be assigned later . The scope of the const is the same as the Let command: only valid at the block-level scope where the declaration resides, and the variable cannot be promoted, there is a "temporary dead zone", can only be declared and then called, and cannot be repeated.
For a variable of a complex type, the variable name does not point to the data, but instead points to the address where the data resides, and the const command simply guarantees that the variable name points to the same address and does not guarantee that the data for that address is constant, so declaring an object as constant must be very careful.
const FOO == 123; Foo.prop// 123// TypeError: "foo" is Read-only
View Code
The constant Foo stores an address that points to an object. Immutable is just this address, that is, you cannot point Foo to another address, but the object itself is mutable, so you can still add new properties for it.
if you want to freeze an object, you can use the Object.freeze method , and once you freeze it, you can't add a new attribute to it, and the strict mode will give you an error.
const FOO = Object.freeze ({}); // Normal mode, the following line does not work;// Strict mode, the row error Foo.prop = 123;
View Code
ES5 There are only two ways to declare variables: the var command and the function command. ES6 in addition to adding the Let and const commands, there are two other ways to declare variables: The import command and the Class command, and then learn the two ways to declare variables later.
ES6 Learn A