做課本練習其中:這個函數的參數是如何傳遞的?
let arr = [1, 2, 3, 4, 5, 6, 7];
function inBetween(a, b) {
return function(x) {
return x >= a && x <= b;
};
}
alert(arr.filter(inBetween(3, 6))); // 3,4,5,6
教科書還指出filter
語法是:
let results = arr.filter(function(item, index, array) {
// should return true if the item passes the filter
});
所以我沒有完全理解inBetween(a,b)
功能是如何工作的......像在這條線:
arr.filter(inBetween(3,6))
在我看來,像a
是item
參數,b
在index
參數中,但顯然這不是它的工作方式......有人可以分解這個語法以及爲什麼它的工作原理?
'x'是' item參數'inBetween'返回一個用於過濾器的函數 – 4castle
'inBetween'是一個*高階函數*,它在調用時返回一個函數* –
參見[瞭解返回函數(x)外面的樂趣ction(陣列)](https://stackoverflow.com/q/36664970/1048572) – Bergi