当前位置:网站首页>ES6 grammar summary -- Part I (basic)

ES6 grammar summary -- Part I (basic)

2022-07-06 12:07:00 Aboci Bang

Variable let

1. There are block level scopes : Variables do not pollute the global scope in the block level scope .
2. There is no claim to promote : Variables can only be used after declaration .
3. Does not affect the scope chain : Variables are normally used in the scope chain .

Constant const

1. The initial value should be assigned when declaring .
2. Constants are generally capitalized .
3. The value of a constant cannot be modified .
4. There are block level scopes .
5. Modifying sub elements of array and object elements is not a constant modification ( Because the address corresponding to the constant has not changed ).
 Insert picture description here

Deconstruct assignment -- Extract values from arrays and objects and assign them to variables

Simple structure

An array of deconstruction :
 Insert picture description here
Object to deconstruct :
notes : Variables declared during deconstruction must exist in the object , Deconstruction can be ignored object key The order of ( Put variables a,b Changing positions can also be deconstructed normally )
 Insert picture description here

Complex structure

Complex structure of objects :
 Insert picture description here
names :
 Insert picture description here

Template string A pair of backquotes Support output line wrapping

For variable splicing
 Insert picture description here
be used for dom Splicing
 Insert picture description here

Object simplification -- If the property name and property value in the object are the same , Can simplify writing

Omit attribute values
 Insert picture description here

Omit anonymous functions
 Insert picture description here

Arrow function =>

How to write it
 Insert picture description here
 Insert picture description here
 Insert picture description here
this
this It refers to the scope of the function declaration and will not be changed .

Arrowhead function this
 Insert picture description here

Of a normal function this
 Insert picture description here
Be careful :

1. Arrow functions cannot be instantiated as construction objects
 Insert picture description here

2. Out of commission arguments Variable
 Insert picture description here
The usage scenarios of arrow function
Apply to : And this Unrelated callbacks , Such as : Timer 、 Array methods
Do not apply to : Event callback ( because this Incorrect pointing )、 Object to declare methods

Parameter default

It is customary to write formal parameters with initial values at the end
The basic way of writing
 Insert picture description here
 Insert picture description here
Combined deconstruction assignment And initial value
 Insert picture description here

rest Parameters Get function arguments

notes :...args Put it at the end of the parameter
 Insert picture description here

Expand operator ( Extension operator ) Convert the array to a comma separated string

Application scenarios

1. Array merge
 Insert picture description here

2. Array Clone -- Deep copy
 Insert picture description here
3. Pseudo array to array (dom object 、arguments etc. )

Technical references :b standing – 【 Silicon Valley 】 I recommend it
https://www.bilibili.com/video/BV1uK411H7on?p=1&vd

原网站

版权声明
本文为[Aboci Bang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913363424.html