Variable declarations

1. let

In fact, let is very similar to VAR, which should be remembered as follows:

Let has block-level scope. For example:

for(let i = 0; i < 3; I ++) {console.log(I) // no problem} console.log(I) // error because I is scoped only within the for loopCopy the code

2. const

Const is used to declare constants, that is, cannot be changed once an assignment is declared. Let applies to const as well

Deconstruction assignment

Destructuring assignments to arrays simplifies writing assignments to arrays

For example, let [a, b, c] = [1, 2,3] console.log(a, b, c) // 1,2,3 have default values: Let [a, b = 2, c] = [1, 3] the console, log (a, b, c) / / 1, 2, 3Copy the code

The deconstructive assignment of objects can also simplify the assignment writing of objects

For example: let [name, age] = ['lee', '22'] let obj1= {name, age} Age} the console. The log (obj1) / / {name: 'lee' age: '22'} have default values: {x, y = 2} = {x: 1, y} / / {x: 1, y: Function add([x = 1, y = 2]) {return x+ y} add([]) // 3 add([2]) // 4 add([, 3]) // 4Copy the code

Template string

Multi-line string concatenation makes it easier to read and write:

`
<div>
<ul>
  <li><p></p></li>
  <li><p></p></li>
</ul>
</div>
`
Copy the code

String template concatenation with variables, simply enclose the variables with ${}

let name = 'lee'
`my name is ${name}` // my name is lee
Copy the code

Extended operators…

let a = [1, 2] console.log(... A) // 1,2 // add new item console.log(... A,3) // 1,2,3 are the same as array.contcat ()Copy the code

Modularize import and export

Easy to split and reuse code, easier to achieve on-demand loading, such as:

// a.js let a = 2 export {a} // b.js import {a} form 'a.js' console.log(a)Copy the code

The class and inheritance

class

There is no class in nature, just a syntactic sugar wrapped around functions…

Class Person (name, age) {constructor (name, age) {// This points to the current Person function, Add and copy the name and age attributes this.name = name this.age = age} this.print() // Method in the Person print() {console.log(this.name) // lee console.log(this.age) // 22 } } new Person('lee', 22)Copy the code

inheritance

Use extends, super

Class Child extends Person (name, age) {constructor (nme, age, sex) {constructor (nme, age, sex) { Age) this.sex = sex} this.print() {console.log(this.name) // lee console.log(this.age) // 22 console.log(this.sex) // male } } new Child('lee', 22, 'male')Copy the code