当前位置:网站首页>Common features of ES6

Common features of ES6

2022-07-05 04:04:00 The boat starts again

1. Different variable declarations :const let and var

constletvar
Scope Yes Yes nothing
Whether it is a variable Is a constant Variable Variable

var No matter where the statement is , Will be considered declared at the top of the function ,

const Declared value is constant , The value of a constant cannot be changed after it is set , But if const What is declared is an object , Object contains values that can be modified . In an abstract sense, the address pointed to by the object does not change .

2. Template string

Basic string formatting , Embed the expression in the string for splicing ${} To define

before :"xxx"+name+"xxx"+age

Now? :`xxx${name}xxx${age}`

3. Arrow function

ES6 Middle arrow function is a short form of function , Wrap parameters in parentheses , Follow a => Next is the function body

The three most intuitive features of arrow function : Unwanted function Keyword to create a function , omitted return keyword , Inherits the current context this Point to ( namely this The direction of will not change )

 

details : When your function has only one parameter, you can omit the parentheses , When your function has and only has one expression, it can be omitted {} and return

4.Spread/Rest The operator ...

rest The parameter form is :... Variable name   rest This is to solve the problem that the number of parameters passed in is not necessarily , Itself is an array , Array related methods can be used

Extension operator : like rest Inverse operation of parameter , Convert an array to use , The parameter sequence separated by is mostly used to merge arrays

 5. Binary and octal literals

ES6 Support binary and 8 The literal amount of base

By adding... Before the number 0O perhaps 0o octal ( turn )0b or 0B Binary system ( turn )

6. Object and array deconstruction

{xxx,xxx,xxx}

7.for...of and for...in

for..of Used to traverse an iterator , Such as arrays

for...in Used to traverse properties in objects

8.ES6 modularization

Modularity mainly includes the following three uses :

Default export and default import export default     import xxx from xxx

Export on demand and import on demand   export xxx    import{xxx,xxx} from xxx

Directly import and execute the code in the module import xxx

9.promise

In order to solve the problem of callback to hell ,ES6 Added in promise, It's a constructor , We can create promise Example const p = new promise(),new Coming out promise Instance object , Represents an asynchronous operation ,promise.propotype Contains a .then() Method , Use a callback function that specifies success and failure in advance .

原网站

版权声明
本文为[The boat starts again]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140713079680.html