下面的java脚本箭头函数如何识别索引?

How the following java script arrow function identify the index?

以下脚本以两种方式转置给定数组。 两者都可以,只是我不明白第二种方法是如何工作的。

//Define array
var a = [[1,2,3],[4,5,6]];

//Transpose
var b = a[0].map((col, i) => a.map(row => row[i]));
console.log(b); //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

//Transpose using callback
b = a[0].map(callbBackFunction(a));
b = a[0].map(callbBackFunction()); //Correction
console.log(b); //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

function callbBackFunction() {        
    return (col, ind) => a.map(row => row[ind]);
}

这两种方法本质上是一样的,只是第二次提取map的callBack函数被提取到了一个外部函数。

我不明白的是 callBackFunction 中的变量 ind 是如何赋值的,因为 callBackFunction 没有定义也没有用参数调用。

地图函数使用当前索引(以及当前项目和映射数组)调用提供的回调,在本例中为 callBackFunction,但 callBackFunction 没有参数接受这个值。

我错过了什么?

*编辑: 这是一个错字:

b = a[0].map(callbBackFunction());

而不是

b = a[0].map(callbBackFunction(a));

callbBackFunctionmap 运行之前执行,实际上是返回其中的函数。该函数在触发 map 之前不会执行,您可以在其中看到它具有与 map 的回调相同的函数签名。

function callbBackFunction() {        
    return (col, ind) => a.map(row => row[ind]);
}

这里有一个例子可以帮助理解这一点:

const fn1 = () => {
     return (name) => "hello " + name
}

// fn1 returns the name func
const nameFn = fn1()

// we can now call nameFn with an argument
nameFn('OJNSim') // hello OJNSim

您可以在此处了解有关 闭包 的更多信息:https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36