A simple list of the small demos commonly used in ES6

Source: Internet
Author: User

# # # Variables related to
Let
Const

1. It is not possible to repeat the statement
2. There are block-level scopes

Const must be assigned at the time of declaration
Const declared variable cannot be modified value

# # # Objects related to
1. Abbreviations for objects
"JS
var name = "King xx";

var obj = {
Name:name
}
Abbreviated as follows
var obj = {
Name
}


var obj = {
Sayhello:function () {

}
}

Abbreviated as follows
var obj = {
SayHello () {

}
}
```

# # # deconstructed Assignment
"JS
1. Deconstruction of objects
var obj = {
Name: "",
Age:18
}
You can automatically assign the value of a property specified in an object to a new variable
var {object's property name: The name of the variable to be declared, the property name of the object: the variable name to declare,}
var {name, age} = obj;

2. The deconstruction of arrays
var arr = [1, 2, 3]
var [num1, num2, num3] = arr;
var [,, num3] = arr;
```

# # # remaining elements
1. There can only be one
2. Must be the last
"JS
var arr = [1, 2, 3]
var [num1, ... num2] = arr;
What num2 gets is an array of the remaining elements 2,3
```

# # # remaining parameters
"JS
You can use the remaining parameters in the argument list of a function to get the arguments passed in
1. There can only be one
2. Must be the last one
3. The remaining parameter is a true array!

function Test (A, ... b) {

}

Test (1, 2, 3, 4, 5)
```

# # # Arrow function
"JS
var func = (parameter) = = {
function body
}
1. If only one argument is omitted ()
2. If the function body has only one line of code then {} omitted
3. If the function body has only one line of code and is a return statement, {} and return can be omitted
```

# # # # arrow function without this
When this is accessed in an arrow function, it is looked up along the scope chain
Usage scenario: var = this; can use the arrow function to solve

# # # # arrows function without arguments
If you want to use an indefinite number of arguments in an arrow function, use the remaining parameters directly

# # # Object extension operator ...
"JS
var p = {
Name: "",
Age:18
}

var s = {... p, own property}
```

# # # Array of disassembly ...
"JS
function sum (A, B, c) {}
var arr = [1, 2, 3];
Sum (... arr)
```

# # Class
"JS
Class Name {
Constructor () {
Adding attributes to an instance
}

Add a method to the instance and the method will be added to the prototype
SayHello () {}

Add a static method to a constructor
static method name () {}
}
```

Implementing inheritance
"JS
Class Person () {
Constructor (name, age) {
THIS.name = name;
This.age = age;
}
SayHello () {}
Static Sayhi () {}
}

Class Student extends person{
Constructor (name, age) {
Super (name, age)
Write your own properties
this. Property name = ""
}

}

var stu = new Studnet ("", 18)
```

# # # Template string
"JS
var name = "King xx"
var age = 45
var str = ' My Name ${name},
I am ${age} years old this year '
```

# # Promise
Promise is used to solve the callback hell problem.

# # # Use the Promise API provided by others
As long as people say they support promise, we know we can use the. Then method.

# # # # # OWN API to encapsulate promise
"JS
function timeout (time) {
return new Promise (function (resolve, reject) {
SetTimeout (function () {
You just need to change the state of the Promise
Resovle (123);
}, Time)
})
}

Timeout (.) then (function () {

})
```

The. Then method can pass two parameters, one is a successful callback and one is a failed callback

The. Then method can be ligatures, but ligatures must return a new Promise object in the previous. Then method.


Promise.all after all the Promise are successful, some operations can be performed
Promise.race after the first Promise is successful, some operations can be performed

A simple list of the small demos commonly used in ES6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.