1.變量聲明
使用const和let代替var
2.塊級作用域
變量只會在當前塊級作用域下生效,不會影響其他的作用域
{
let tmp='';
}
3.模板
{}
4.箭頭表達式
()沒有參數(shù)時需要加()=>{}
只有一個參數(shù)可以不加() index=>{}
一個以上的需要加()=>{}
5.for of
const arr = ['a','b','c'];
for(const elem of arr){
console.log(elem);
}
6.默認參數(shù)
function (x=0,y=0){
...
}
function ({x=0,y=-1}){
}
//無限的參數(shù)
function (...args){
for (const elem of args){
console.log(elem);
}
}
Math.max.apply(Math, [-1, 5, 11, 3])
=》
Math.max(...[-1, 5, 11, 3])
arr1.push.apply(arr1, arr2);
=》
arr1.push(...arr2);
console.log(arr1.concat(arr2, arr3));
=》
console.log([...arr1, ...arr2, ...arr3]);
對象字面量
省略掉了function()
var obj = {
foo:{
...
},
bar:{
this.foo();
},
}
類
省略掉了function,各部分不需要逗號
class Person(){
constructor(name){
this.name = name;
}
describe(){
return 'Person called' + this.name;
}
}
多項導出
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
//------ main1.js ------
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
//------ main2.js ------
import * as lib from 'lib'; // (A)
console.log(lib.square(11)); // 121
console.log(lib.diag(4, 3)); // 5