javascript - 无法理解这些功能
问题描述
//iterator(value, key, collection)
_.each = function(collection, iterator) { //takes in collection of values and a functino called iterator
if(Array.isArray(collection)) { //if collection is array, proceed
for (var key = 0; key < collection.length; key++) { //iterates over the length of collection
iterator(collection[key], key, collection);//plugs in individual element value, index of element, and array var
}
} else {
for (var objKey in collection) {
iterator(collection[objKey], objKey, collection);
}
}
};
_.indexOf = function(array, target){
var result = -1; //set result to -1
_.each(array, function(item, index) {
if (item === target && result === -1) {
result = index;
}
});
return result;
};
//example of input
_.indexOf = function([1, 2, 3], 2)
我无法理解这些函数。特别是在 ._indexOf 变量赋值中使用 _.each 函数的位置。在执行 ._each(array, function(item, index) 的代码行中。该函数的结果是然后用于插入 function(item, index) 的函数还是相反?如何它知道该变量的项目和索引参数是什么?
解决方案
推荐阅读
- python - 如何压缩大于 Python 中可用内存的列表?
- sql - 如何连接3张桌子?
- c++ - 如何制作菜单以将用户输入数据保存或检索到文件?
- mysql - 如何使用下一个非空值填充空列值
- alloy - Alloy 中的断言和未参数化谓词有什么区别?
- nosql - Oracle NoSQL 数据库云服务是否支持命名空间?
- python - 类型错误:info() 缺少 1 个必需的位置参数:'state'
- swift - Firebase 说“提供的链接对电子邮件/链接登录无效。通过调用 isSignInWithEmailLink 检查链接”
- apache-spark - Spark join 删除连接列
- python-3.x - 使用 pandas 从 CSV 文件中重新采样日期