匿名函數(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]