做课本练习其中:这个函数的参数是如何传递的?
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