一梦碗、Rest and Spread操作符
1禽绪、用來聲明可以傳入任意數(shù)量的參數(shù)的方法
2、可以把任意長度的數(shù)組轉(zhuǎn)化成任意長度參數(shù)方法的調(diào)用(es6規(guī)范洪规,typescript還不支持)
二印屁、generator函數(shù)(es6規(guī)范,typescript還不支持)
該類型函數(shù)可以控制函數(shù)的執(zhí)行過程斩例,手工暫停和恢復(fù)代碼執(zhí)行
使用babel編輯器演示:http://babeljs.io/repl/
函數(shù)執(zhí)行到y(tǒng)ield的時(shí)候停止執(zhí)行雄人,調(diào)用next()方法時(shí)再繼續(xù)執(zhí)行。
三念赶、destructuring析構(gòu)表達(dá)式
作用:通過表達(dá)式將對象和數(shù)組拆解成為任意數(shù)量的變量
function getStock() {
return {
code: "IBM",
price:100
}
}
//es5寫法
var stock = getStock()
var code = stock.code
var price = stock.price
//es6寫法(析構(gòu)表達(dá)式)
var {code,price} =getStock()
console.log(code,price)
若要給解構(gòu)出來的變量重命名則:
function getStock() {
return {
code: "IBM",
price:100
}
}
var {code:codex,price} =getStock()
console.log(codex,price)
若要取一個嵌套屬性:
function getStock() {
return {
code: "IBM",
price:{
price1:200,
price2:400,
}
}
}
var {code:codex,price:{price2}} =getStock()
console.log(codex,price2)
析構(gòu)表達(dá)式從數(shù)組里面取值:
var arr1 = [1, 2, 3, 4]
var [number1, number2] = arr1
console.log(number1)//1
console.log(number2)//2
var [,,number1, number2] = arr1
console.log(number1)//3
console.log(number2)//4
var [number1,,,number2] = arr1
console.log(number1)//1
console.log(number2)//4
//析構(gòu)表達(dá)式和rest操作符相結(jié)合
var [number1,number2,...others] = arr1
console.log(number1)//1
console.log(number2)//2
console.log(others)//[3,4]
//函數(shù)中用析構(gòu)表達(dá)式
function doSomething([number1,number2,...others]) {
console.log(number1)//1
console.log(number2)//2
console.log(others)//[3,4]
}
doSomething(arr1)