ES6 learn notes

Wherever interview, you will not avoid es6, then as a program ape, decided to study under es6, their learning ability is poor, very slow to learn something, to learn for a long time, just a little clue, the line did not talk much , quickly put up notes on!

1. The first is the wording of the definition of variables, can be repeated until a var statement, let ---- now have block-level variable definition can not be repeated statement that is defined within the function, then it can only be used within the function, not the global, then it is less than, this benefit is to prevent data pollution; const definitions for fixed, such as domain name, address or something;

2. arrow function - this I began to think good trouble, so hard, but learned later found that lecturers would say the same is the function of shorthand,

Previous wording 

let a=function(){}

Arrow function

let a=()=>{return a}

If there is only one parameter {}, {} may be omitted

let a=()=>return a;

Correcting this, relatively normal point

3. function parameters

Is a general pattern of function a (a, b, c, d)

You can now write function (a, b, ... args) ------ (... args) represents the remaining parameters but only as a last argument can be extended to collect

let data=[12,5,8,9];

let arr = [... data, .. data]; Extended

4. deconstruction assignment

Such as defining a variable let a = 10, b = 2;

You can write to let [a, b] = [10,2], what type of left and right correspond to the type of assignment

5. array

Array es6 more than four methods ---

map, mapping a one of [1,6,5,4] can be mapped to [in January, June, May, April];

let result = Array.map (item => Data)

For example let data = [20,60,80,50] ----- data.map (? Item => item> 60 'pass': 'fail');

 

reduce, summary

let data=[20,60,80,50];

Results item-- tmp-- calculated value, index-- array index

data.reduce((tmp,item,index)=>{

   if(index!data.length-1){

      return tmp + item; plus a first value and a second value, and processing is continued if the value of the third .......

     }else{

 }

})

 

filter, leaving a portion of the filter puncturing part ----

let data=[20,60,80,50];

// returns the data is divisible by 3 of true; data.filter (item => item% 3 == 0)

 

forEach, loop (iteration)

let data=[20,60,80,50];

data.forEach((item,index)=>{

  Operation data portion

})

 

6. string

More than two new methods startsWith endsWith;

let data="sadhkjashdkj";

data.startWith ( 's') // first string is determined not begin with s

data.endsWith ( 'j') // first string is determined not to end j

------------- can be used to determine mail and web site

String template

Previous string concatenation is then str = '', str + = '.........'

You can now be written as: str = `` If you have content you want to insert the variable value with $ {variable name} can be inserted

 

7.JSON

JSON object

let json = {a: 12, b: 5}; --- json wording

JSON.stringify (json) ---- becomes a string

let json="{"a":12,"b":23}"

Standard wording - only with all the names must have double quotes double quotes wrap, or will be error

JSON.parse(json)---变成json

 

JSON shorthand

json name with the value (key and value) the same as when you can just write a let json = {a: a} =========== let json = {a}

 

8.promise

We are generally asynchronous data synchronization request

Asynchronous: multiple requests at the same time, the code becomes complicated;

Synchronization: You can only do one thing, the code is simple;

Both strengths and weaknesses, so there is a promise to eliminate asynchronous, synchronous operation code of the method

General wording:

resolve-- success reject-- failure

let p=new Promise(function(resolve,reject){

For example, request here

$.ajax({

url:'.........',

dataType:'json',

success(data)=>{

  resolve(data)

},error(error)=>{

  reject(error)

}

})

})

Call words: p.then (function () {}, --- the successful function function () {} --- failure function)

If multiple requests at the same time, it may be used Promise.all ([Request 1, Request 2]). Then ()

You may encapsulate promise

GreatePromist function // (URL) {
// return new new Promise (function (Resolve, Reject) {
// $ .ajax (URL, {
// dataType: 'json', // server returns json data format
// type: ' post ', // HTTP request type
// timeout: 10000, // timeout is set to 10 seconds;
// success: function (Data) {
// Resolve (Data); // successful return
//},
// error : function (error) {
// Reject (error); // else return
//}
//});
//})
//}

Then call is Promise.all ([Request 1, Request 2]). Then ()

In fact, jquery package itself has promise in version 3. More than

So we can Promise.all ([$ ajax ({url1, dataType: 'json'}), $ ajax ({url2, dataType: 'json'}),..]) Then ();.

 

9.generator generator

Request for logic, do half the things you can pause

Detailed  https://www.liaoxuefeng.com/wiki/1022910821149312/1023024381818112

To es7 when this method can be used instead of async await specific can first look at Baidu

 

Guess you like

Origin www.cnblogs.com/aotuboke/p/11096158.html