深入學(xué)習(xí)JavaScript 高階函數(shù)
高階函數(shù)
高階函數(shù)英文叫 Higher-order function,它的定義很簡單,就是至少滿足下列一個條件的函數(shù):
- 接受一個或多個函數(shù)作為輸入
- 輸出一個函數(shù)
也就是說高階函數(shù)是對其他函數(shù)進行操作的函數(shù),可以將它們作為參數(shù)傳遞,或者是返回它們。 簡單來說,高階函數(shù)是一個接收函數(shù)作為參數(shù)傳遞或者將函數(shù)作為返回值輸出的函數(shù)。
函數(shù)作為參數(shù)傳遞
JavaScript 語言中內(nèi)置了一些高階函數(shù),比如 Array.prototype.map,Array.prototype.filter 和 Array.prototype.reduce,它們接受一個函數(shù)作為參數(shù),并應(yīng)用這個函數(shù)到列表的每一個元素。我們來看看使用它們與不使用高階函數(shù)的方案對比。
Array.prototype.map
map() 方法創(chuàng)建一個新數(shù)組,其結(jié)果是該數(shù)組中的每個元素都調(diào)用一個提供的函數(shù)后返回的結(jié)果,原始數(shù)組不會改變。傳遞給 map 的回調(diào)函數(shù)(callback)接受三個參數(shù),分別是 currentValue、index(可選)、array(可選),除了 callback 之外還可以接受 this 值(可選),用于執(zhí)行 callback 函數(shù)時使用的this 值。
來個簡單的例子方便理解,現(xiàn)在有一個數(shù)組 [1, 2, 3, 4],我們想要生成一個新數(shù)組,其每個元素皆是之前數(shù)組的兩倍,那么我們有下面兩種使用高階和不使用高階函數(shù)的方式來實現(xiàn)。
不使用高階函數(shù)
// 木易楊 const arr1 = [1, 2, 3, 4]; const arr2 = []; for (let i = 0; i < arr1.length; i++) { arr2.push( arr1[i] * 2); } console.log( arr2 ); // [2, 4, 6, 8] console.log( arr1 ); // [1, 2, 3, 4]
使用高階函數(shù)
// 木易楊 const arr1 = [1, 2, 3, 4]; const arr2 = arr1.map(item => item * 2); console.log( arr2 ); // [2, 4, 6, 8] console.log( arr1 ); // [1, 2, 3, 4]
Array.prototype.filter
filter() 方法創(chuàng)建一個新數(shù)組, 其包含通過提供函數(shù)實現(xiàn)的測試的所有元素,原始數(shù)組不會改變。接收的參數(shù)和 map 是一樣的,其返回值是一個新數(shù)組、由通過測試的所有元素組成,如果沒有任何數(shù)組元素通過測試,則返回空數(shù)組。
來個例子介紹下,現(xiàn)在有一個數(shù)組 [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4],我們想要生成一個新數(shù)組,這個數(shù)組要求沒有重復(fù)的內(nèi)容,即為去重。
不使用高階函數(shù)
const arr1 = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]; const arr2 = []; for (let i = 0; i < arr1.length; i++) { if (arr1.indexOf( arr1[i] ) === i) { arr2.push( arr1[i] ); } } console.log( arr2 ); // [1, 2, 3, 5, 4] console.log( arr1 ); // [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]
使用高階函數(shù)
const arr1 = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]; const arr2 = arr1.filter( (element, index, self) => { return self.indexOf( element ) === index; }); console.log( arr2 ); // [1, 2, 3, 5, 4] console.log( arr1 ); // [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]
Array.prototype.reduce
reduce() 方法對數(shù)組中的每個元素執(zhí)行一個提供的 reducer 函數(shù)(升序執(zhí)行),將其結(jié)果匯總為單個返回值。傳遞給 reduce 的回調(diào)函數(shù)(callback)接受四個參數(shù),分別是累加器 accumulator、currentValue、currentIndex(可選)、array(可選),除了 callback 之外還可以接受初始值 initialValue 值(可選)。
- 如果沒有提供 initialValue,那么第一次調(diào)用 callback 函數(shù)時,accumulator 使用原數(shù)組中的第一個元素,currentValue 即是數(shù)組中的第二個元素。 在沒有初始值的空數(shù)組上調(diào)用 reduce 將報錯。
- 如果提供了 initialValue,那么將作為第一次調(diào)用 callback 函數(shù)時的第一個參數(shù)的值,即 accumulator,currentValue 使用原數(shù)組中的第一個元素。
來個簡單的例子介紹下,現(xiàn)在有一個數(shù)組 [0, 1, 2, 3, 4],需要計算數(shù)組元素的和,需求比較簡單,來看下代碼實現(xiàn)。
不使用高階函數(shù)
const arr = [0, 1, 2, 3, 4]; let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } console.log( sum );
// 10
console.log( arr );
// [0, 1, 2, 3, 4]
使用高階函數(shù)
無 initialValue 值
const arr = [0, 1, 2, 3, 4]; let sum = arr.reduce((accumulator, currentValue, currentIndex, array) => { return accumulator + currentValue; }); console.log( sum ); // 10 console.log( arr ); // [0, 1, 2, 3, 4]
上面是沒有 initialValue 的情況,代碼的執(zhí)行過程如下,callback 總共調(diào)用四次。
callback | accumulator | currentValue | currentIndex | array | return value |
---|---|---|---|---|---|
first call | 0 | 1 | 1 | [0, 1, 2, 3, 4] | 1 |
second call | 1 | 2 | 2 | [0, 1, 2, 3, 4] | 3 |
third call | 3 | 3 | 3 | [0, 1, 2, 3, 4] | 6 |
fourth call | 6 | 4 | 4 | [0, 1, 2, 3, 4] | 10 |
有 initialValue 值
我們再來看下有 initialValue 的情況,假設(shè) initialValue 值為 10,我們看下代碼。
const arr = [0, 1, 2, 3, 4]; let sum = arr.reduce((accumulator, currentValue, currentIndex, array) => { return accumulator + currentValue; }, 10); console.log( sum ); // 20 console.log( arr ); // [0, 1, 2, 3, 4]
代碼的執(zhí)行過程如下所示,callback 總共調(diào)用五次。
callback | accumulator | currentValue | currentIndex | array | return value |
---|---|---|---|---|---|
first call | 10 | 0 | 0 | [0, 1, 2, 3, 4] | 10 |
second call | 10 | 1 | 1 | [0, 1, 2, 3, 4] | 11 |
third call | 11 | 2 | 2 | [0, 1, 2, 3, 4] | 13 |
fourth call | 13 | 3 | 3 | [0, 1, 2, 3, 4] | 16 |
fifth call | 16 | 4 | 4 | [0, 1, 2, 3, 4] | 20 |
函數(shù)作為返回值輸出
這個很好理解,就是返回一個函數(shù),下面直接看兩個例子來加深理解。
isType 函數(shù)
我們知道在判斷類型的時候可以通過 Object.prototype.toString.call 來獲取對應(yīng)對象返回的字符串,比如:
let isString = obj => Object.prototype.toString.call( obj ) === '[object String]'; let isArray = obj => Object.prototype.toString.call( obj ) === '[object Array]'; let isNumber = obj => Object.prototype.toString.call( obj ) === '[object Number]';
可以發(fā)現(xiàn)上面三行代碼有很多重復(fù)代碼,只需要把具體的類型抽離出來就可以封裝成一個判斷類型的方法了,代碼如下。
let isType = type => obj => { return Object.prototype.toString.call( obj ) === '[object ' + type + ']'; } isType('String')('123'); // true isType('Array')([1, 2, 3]); // true isType('Number')(123); // true
這里就是一個高階函數(shù),因為 isType 函數(shù)將 obj => { ... } 這一函數(shù)作為返回值輸出。
add 函數(shù)
我們看一個常見的面試題,用 JS 實現(xiàn)一個無限累加的函數(shù) add,示例如下:
add(1); // 1 add(1)(2); // 3 add(1)(2)(3); // 6 add(1)(2)(3)(4); // 10 // 以此類推
我們可以看到結(jié)構(gòu)和上面代碼有些類似,都是將函數(shù)作為返回值輸出,然后接收新的參數(shù)并進行計算。
我們知道打印函數(shù)時會自動調(diào)用 toString()方法,函數(shù) add(a) 返回一個閉包 sum(b),函數(shù) sum() 中累加計算 a = a + b,只需要重寫sum.toString()方法返回變量 a 就可以了。
function add(a) { function sum(b) { // 使用閉包 a = a + b; // 累加 return sum; } sum.toString = function() { // 重寫toString()方法 return a; } return sum; // 返回一個函數(shù) } add(1); // 1 add(1)(2); // 3 add(1)(2)(3); // 6 add(1)(2)(3)(4); // 10
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
有關(guān)JavaScript中call()和apply() 的一些理解
下面小編就為大家?guī)硪黄嘘P(guān)JavaScript中call()和apply() 的一些理解。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05window.navigate 與 window.location.href 的使用區(qū)別介紹
首先說明的是 window.navigate 與 window.location.href 都是實現(xiàn)頁面鏈接跳轉(zhuǎn)的,下面將介紹它們的區(qū)別。感興趣的朋友可以參考下2013-09-09JavaScript前端圖片加載管理器imagepool使用詳解
這篇文章主要介紹了JavaScript前端圖片加載管理器imagepool使用詳解,需要的朋友可以參考下2014-12-12javascript中hasOwnProperty() 方法使用指南
這篇文章主要詳細介紹了javascript中hasOwnProperty() 方法使用指南,非常的全面,推薦給有需要的小伙伴參考下。2015-03-03你必須知道的JavaScript 中字符串連接的性能的一些問題
每種程序語言中都會涉及到字符竄連接,而這個小小的字符竄連接問題很可能會影響到系統(tǒng)的整體性能,本文主要探討JavaScript中字符串連接的性能問題2013-05-05