ES6 Grammar (1) Knowledge Summary

One, block scope

In ES6, there is a saying of block scope, such as this

{
	...代码...
}

After writing the code in '{}', the life cycle of the code will be valid within the scope of '{}', and it has the function of block scope for the variables defined by the let and const keywords. Once outside this range, you will not find variables defined by let or const. Because var is a function scope variable, var is not restricted by block scope.

Error report:

Code>

var a=1;
{
let b=2;
console.log(a);//第一次打印a
console.log(b);//第一次打印b
}
console.log(a);//第二次打印a
console.log(b);//第二次打印b
Operation result>


It can be seen that the scope of b is within {} brackets, there is no error in {} in the first printing, the output result is the same as the initial value, and the error is reported in the second printing.
But in the two prints, the variable defined by var has no error, and both output its initial value normally.
This is the scope of the block. The scope of the variables defined by let and const can only be performed in {}, which is limited by the block.

Second, let and const

1,let

Let and var are very similar, their common role is to define variables, such as:

var a=1;
let a=1;

The same is to assign a value of 1 to the variable a

Use the let keyword to pay attention to two points:
1. Variable declaration cannot be promoted (Once a variable is defined, it must be defined before use, and the definition of the variable after use will report an error, but the var keyword can do it)
2. The variable cannot be declared repeatedly

2,const

const is used to define constants. The
defined constants cannot be changed, for example:

const a=1;
a=2;

The running result is as follows:

after a is defined, it is a fixed value and a constant. If you change, you will get an error.

Use the const keyword to pay attention to two points
1. There is an initialization value
2 when it is defined . The initialization value cannot be changed (binding cannot be changed)

Third, the template string

The use of template strings, and the
line breaks within the symbol can be freely reported without errors
such as:

let kokk='';
kokk=`
<li>${2333}
</li>
`

The results are as follows:

In the code above table Show of Yes spell Pick up word symbol string of meaning think {} Means the concatenation of strings, equivalent to + {} which represents the contents to be written.

Four, arrow function

The arrow function is made with () => {}, which is the function pointed by the arrow, which greatly simplifies the writing of this form of function () {} (less a few letters, laziness can be)

const aa=()=>{
console.log(233);
}
aa();

The running result is as shown in the figure: the

printing result is 233, which is consistent with the code in the function.

Note the use of arrow functions:
1, the problem pointed by this (no this, with the help of the parent this)
2, can not use arguments
3, can not be used as a constructor

Come on!

Published 16 original articles · Liked 14 · Visits 3639

Guess you like

Origin blog.csdn.net/qq_41136216/article/details/105520300