name

匿名函數(shù)(Lambdas) 箭頭函數(shù) =>

const lamb = (a) => parseInt(a)  * 10

頭等函數(shù)(first-class Function

  • 可被賦值給變量、數(shù)列元素和對象屬性
  • 函數(shù)可作為參數(shù)使用
  • 可以當(dāng)做返回值
const handler = () => console.log ('click once The Fn');

document.addEventListener ('click', handler); 

高階函數(shù)(higher-order functions

  • 可以將其他函數(shù)作為參數(shù),或?qū)⒁粋€函數(shù)作為返回
const handler = () => console.log ('click once The Fn');

document.addEventListener ('click', handler); 

一元函數(shù)(unary functions

  • 只接受一個參數(shù)的函數(shù)
const unaryFn = message => console.log (message);


柯里化(currying

  • Currying(柯里化)是一個帶有多個參數(shù)的函數(shù)并將其轉(zhuǎn)換為函數(shù)序列的過程,每個函數(shù)只有一個參數(shù)。

一個有n個參數(shù)的函數(shù),可以使用柯里化將它變成一個一元函數(shù)。

const binaryFunction = (a, b) => a + b;
const curryUnaryFunction = a => b => a + b;

curryUnaryFunction (1); // returns a function: b => 1 + b
curryUnaryFunction (1) (2); // returns the number 3

純函數(shù)(pure functions

  • 純函數(shù)是一種其返回值僅由其參數(shù)決定,沒有任何副作用的函數(shù)。

這意味著如果你在整個應(yīng)用程序中的不同的一百個地放調(diào)用一個純函數(shù)相同的參數(shù)一百次,該函數(shù)始終返回相同的值。純函數(shù)不會更改或讀取外部狀態(tài)。

let myArray = [];

const impureAddNumber = number => myArray.push (number);

const pureAddNumber = number => anArray =>
  anArray.concat ([number]);
  
console.log (impureAddNumber (2)); // returns 1

console.log (myArray); // returns [2]

console.log (pureAddNumber (3) (myArray)); // returns [2, 3]

console.log (myArray); // returns [2]

myArray = pureAddNumber (3) (myArray);

console.log (myArray); // returns [2, 3]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容