ES6 new contents are: 1, modular thinking. 2, About variables and let const. 3, deconstruction assignment. 4, the extended string. 5, the extended functions. 6, arrows function. 7, inherited apply usage
The following is a breakdown:
1: modularization
There are non-modular naming conflicts, files rely shortcomings, modular thinking to solve this problem, a file that is a module module export, through exports and module.exports.
export:
In a module file can have multiple export interface module for exposure, but only one export default default interface module for exposure, for example:
export const myStr = 'hello world'
export const myNum = 1000
export const myBoole = false
export default {
id: 1,
title: 'this is title',
}
import:
When introduced into the module through the interface exposed export needed wrap {}, the interface exposed by export is not, for example:
// introduction default output objects
Import from defaultObj './test.js' // introducing a plurality of output variables
Import {myStr, myNum, myBoole} from './test.js' // while introducing the default output and a plurality of output objects variable
import defaultObj {myStr, myNum, myBoole } from './test.js'
var module=require('filePath');
-
Suffixes module file: .js .json .node
Load priority (without writing suffix) .js -.> Json -.> Node
2. About variables and let const summary
Let preresolved variable declaration does not exist can not be duplicated at the block level scope declarations, variable block-level and let inaccessible outside the scope of the declared const. const is used to declare lit, do not allow re-assignment, must be initialized when declared
3. deconstruction assignment
/ * Structure assignment variables * /
// structure assignment array
the let [A, B, C, D] = [1, 2,3,5 ];
the console.log (A, B, C, D);
/ * structure assignment objects * /
/ * the let {name, Age} = {name: 'Bob', Age: 20 is};
the console.log (name, Age); * /
// object properties alias
let {name: firstName, age } = {name: 'Bob', Age: 20 is };
the console.log (firstName, Age);
the let {SiN, COS} = the Math;
the console.log ( typeof SiN);
the console.log ( typeof COS);
/ * structure assignment string * /
the let [H, G, J] = 'jefiwi' ;
the console.log (H, G, J);
4. extended string
includes () // determines whether a string contains the character string, the first string of a parameter to be estimated, the second parameter is the index number from the start. Here is a simple example
var arr = "abcdefghi";
console.log(arr.includes("j"))
//false
console.log(arr.includes("d"))
//true
console.log(arr.includes("d",5))
//false
console.log(arr.includes("d",3))
//true
The second argument can not write, the default is 0
there is a template string
If you say that now there is a list, a list of the contents of the cycle is derived, this time we will write in the inner loop structure tag
var arr = ['11','22','33'];
var str = "";
for(var i = 0;i<arr.length;i++){
str +=`
<li>${arr[i]}</li>
`
};
console.log(str)
/*
*<li>11</li>
*<li>22</li>
*<li>33</li>
*/
At this time, we can put str, on where we want the
5. The extension function
1. The default value of 2. The parameter destructuring assignment, 3.rest 4 ... the extension parameter.
1 // Default
2 function Print (A = 10, B = 2, C = 1 ) {
. 3 the let Result = A + B + C;
. 4 the console.log (Result);
. 5 }
. 6 Print ();
. 7 // deconstruction assignment
. 8 function foo ({name = 'Bob', age = 18 is} = {}) {
. 9 the console.log (name, Age);
10 }
. 11 foo ();
12 is foo ({name: 'red', age : 20 is });
13 is // REST parameter
14 function restParam (A, B, ... param) {
15 the console.log (A);
16 the console.log (B);
. 17 the console.log (param);
18 is . 19 }
20 is restParam (1,2,3,56,6,9,45,12 );
21 is . 1
22 is 2
23 is [. 3, 56 is,. 6,. 9, 45, 12 is ]
24 // ... the extension into a single array parameter in the function call. 25 function restParam (A, B, C, D, E) {
26 is the console.log (A + B + C + D + E);
27 28 }
29 the let ARR = [20,30,45,56,30 ]
30 restParam (ARR ...);
31 is // merge array 32 the let of arr1 = [12,5 ];
33 is the let arr2 is = [45,89
];
34 let arr3=[...arr1,...arr2]
35
6. Arrow function
. 1 the let foo = (A, B) => {
2 the console.log (A + B)
. 3 };
. 4 foo (10,20 )
. 5 // above written equivalent
. 6 function foo (A, B) {
. 7 return Console .log (A + B)
. 8 };
. 9 foo (10,20)
Arrow function Notes
1. the this depends on the definition of the function, instead of calling.
2. arrow function without the new keyword
3. arrow function arguments can not be used to obtain the parameter list, you can use the rest parameters instead of
7.apply
grammar | definition | Explanation |
---|---|---|
call(thisObj,Object) | A method invocation of an object to another object to replace the current object. | The method may be used instead call another object calls a method. The method may call a function of the object context specified by the new object from the initial thisObj context change. If no thisObj parameters, then the object is used as Global thisObj |
apply(thisObj,[argArray]) | Application of a method of an object, replacing the current object with another object. | If argArray not a valid arguments object array or not, it will cause a TypeError. If no parameter argArray any thisObj, then the object is used as Global thisObj, and can not be passed any parameters |