//原本的函式functionadd(x, y, z){return x+y+z}//改寫1functionaddXY(z){returnadd(1,2, z)}addXY(3)//改寫2functionadd(x, y, z){returnfunction(z){return x+y+z }}constaddXY=add(1,2)addXY(3)
柯里化
//原本的函式functionadd(x, y, z){return x+y+z}//柯里化functionadd(x, y, z){returnfunction(y){returnfunction(z){return x + y + z } }}add(1)(2)(3)
//傳入沒有名稱的物件,直接解構functionfunc({a, b}) {return a + b}func({a:1, b:2}) // 3
generator
可中斷的函數,且可恢復執行
每次執行一個區間的代碼,分段執行,且異步調用
遇到yield就停止,傳回執行結果
*表示此為generator
//例一function*gen(){yield1yield2return3}//非回傳3,而是產生iterator objectlet g=gen()g.next() //{value:1,done:false}g.next() //{value:2,done:false}g.next() //{value:3,done:true}g.next() //{value:undefined,done:true}//例二function*demo(){yield a =yieldPromise.resolve(1)yield b =yieldPromise.resolve(a+2)yield c =yieldPromise.resolve(3+b)return c}