es6 addition to modular, class, promise, there are some other common functions.
1、let/const
let is to define a variable, but this variable can be reassigned, const is to define a constant, this constant can not be reassigned
I = 10 the let; I = 100; // correct const = 20 is J; J = 200 is; // given
2, multi-line strings / template variables
// JS var name = 'zhangsan', age = 20, html = ''; html = '<div>' html += ' <p>' + name + '</p>'; html += ' <p>' + age + '</p>'; html +='</div>' // es6 const name = 'zhangsan', age = 20; const html = ` <div> <p>${name}</p> <p>${age}</p> </div> `;
A characteristic anti quotes, introducing a $ {variable}, the code read
3, deconstruction assignment
// JS var obj = {a:100, b:200} var a = obj.a; var b = obj.b; var arr = ['xxx', 'yyy', 'zzz']; var x = arr[0]; //ES6 const obj = {a:10, b:20, c:30}; const {a, c} = obj; console.log(a); console.log(c); const arr = ['xxx', 'yyy', 'zzz']; const [x, y, z] = arr; console.log(x); console.log(y); console.log(z);
4, block-level scope
js no block-level scope, will be buried pit
// JS var obj = {a:100, b:200} for (var item in obj){ console.log(item); } console.log(item) // b // ES6 const obj = {a:100, b:200} for(let item in obj){ console.log(item); } console.log(item);// undefined
5, the default function arguments
//JS function fn(a,b){ if (b == null) { b = 0; } } //ES6 function fn(a, b=0){ }
6, arrows function
//JS var arr = [1, 2, 3, 4, 5]; arr.map(function(item){ return item + 1; }) //ES6 const arr = [1, 2, 3, 4, 5]; arr.map(item => item +1); function fn(){ console.log('real', this); // {a:100} var arr = [1, 2, 3]; arr.map(() => { console.log(this); // {a:100} }) arr.map(function(){ console.log(this) }) } fn.call({a: 100})