javascript - 为什么reduce中谓词函数需要包装函数?
问题描述
我正在玩 Array.reduce 和 Set 之间的交互,我注意到以下奇怪的行为。
通常这是有效的:
console.log(
Set.prototype.add.call(new Set(), 1, 0, [])
);
// Set { 1 }
但是,如果我将其与 reduce 结合起来,则以下内容不起作用:
console.log(
[1,2,3].reduce(Set.prototype.add.call, new Set())
);
// TypeError: undefined is not a function
// at Array.reduce (<anonymous>)
但是,如果我将谓词函数包装在包装器中,这将起作用:
console.log(
[1,2,3].reduce((...args) => Set.prototype.add.call(...args), new Set())
);
// Set { 1, 2, 3 }
我在不同的 JS 引擎(Chrome 和 Safari)上尝试了这个并得到了相同的结果,所以它可能不是引擎特定的行为。这同样适用于 Map 对象。我无法弄清楚为什么会这样。
解决方案
没有包装,你Set.prototype.add.call
会失去它的this
价值(应该是Set.prototype.add
函数,而是设置为undefined
)。
尝试这个:
[1,2,3].reduce(Set.prototype.add.call.bind(Set.prototype.add), new Set());
推荐阅读
- r - 格式化分面饼图ggplot
- filepond - 如何使用 filepond 获取 exifdata(如 gps)?
- go - go encoding/pem 删除空白字符
- javascript - 如何添加猫鼬事务并创建文档?
- database - Pojo 类中对象列表的 Mybatis 属性映射器错误:spring Boot/mybatis
- kubernetes - Kubernetes v1.21.2:“selfLink 为空,无法引用”
- javascript - 矩阵变量在javascript中未定义
- tensorflow - 使用“tf.compat.v1.metrics.auc”的最佳实践?
- android - 模拟器或物理设备的 AOSP Android 12 Beta2 版本构建失败
- python - 使用 seaborn 创建多条曲线