javascript - 像`map()`和`reduce()`这样的高阶函数如何接收它们的数据?
问题描述
我现在正在尝试编写自己的高阶函数,我想知道函数如何喜欢map()
和reduce()
访问它们被应用到的数组。不仅适用于数组,还适用于任何高阶函数,如toString()
or toLowerCase()
。
array.map()
^^^ // How do I get this data when I am writing my own higher order function?
array.myOwnFunction(/* data??? */)
我希望这是有道理的。我确定答案已经存在,但我很难知道要搜索什么来查找信息。
解决方案
您可以将其添加到Array
原型中,例如:
Array.prototype.myOwnFunction = function() {
for (var i = 0; i < this.length; i++) {
this[i] += 1;
}
return this;
};
const array = [1, 2, 3];
const result = array.myOwnFunction();
console.log(result);
推荐阅读
- python - 追加到字典中的一个列表而不是追加到所有列表
- powershell - 在特定时间结束 Powershell 作业
- git - 用 git 清理混乱的分支
- visual-studio - dotnet build 静默失败
- php - 如何在数组的同一行显示两列数据?
- java - junit测试中的Java.lang.Assertion错误
- javascript - 保存到移动设备上的本地存储 [React]
- reactjs - 是否可以从 React 路由中获取密钥以传入组件?
- ios - 如何在 snapkit 中更改 offset() 和 multipliedBy() 的优先级
- reactjs - 如何在本机反应中设置firebase实时数据库事件监听器