ES6基礎(1)
阿新 • • 發佈:2018-11-06
1. let,var和const
- var定義的變數函式級區域全部有效,會自動進行變數提升,
- let和const是塊級作用域
- const定義常量不可更改
2.模板語言
- 例1:
var str =
`模板程式碼`
- 例2:
let name = "Ann"; console.log(`I am ${ name }`);
輸出結果:I am Ann
3.預設引數
在函式引數中可以設定(=)預設值
4.箭頭函式
sum.map( (num) => { num + 1 ;}); //sum.map( (引數) => { 程式碼或返回值; })
5.解構
拆分字串,陣列,物件,函式等
//陣列,字串,物件
var [a,b,c] = [1,2,3];//a=1,b=2,c=3
var [x,y,z] = "abc";
var {m,n} = { m:10,n:20};
6.Rest引數和擴充套件
Rest引數:類似於Java的可變長引數,在引數不確定數量的情況下。。。
function sum2(x,y,...m) { let total = x+y ; for (let i of m) { total += i; } console.log(`total:${total}`); }
sum2(1,2,6);//結果:total:9
箭頭函式寫法
let sum3 = (n,...m)=>{
let total = n ;
for (let i of m) {
total += i;
}
console.log(`total:${total}`);
}
擴充套件:拆解陣列等
console.log(...[1,2],..."abc");
//輸出:1 2 "a" "b" "c"