题目描述在看lodash源码的时候看到createSet函数时存在这样一个判断 Set && (1 / setToArray(new Set([,-0]))[1]) == INFINITY 不太理解后半段的(1 / setToArray(new Set([,-0]))[1]) == INFINITY的意思相关代码const createSet = (Set && (1 / setToArray(new Set([,-0]))[1]) == INFINITY)
? (values) => new Set(values)
: () => {}
1 回答

梦里花落0921
TA贡献1772条经验 获得超6个赞
对于 Set
来说, -0
和 +0
是有区别的, 但在 es2015 后规范指定 -0 和 +0 相等. 存在浏览器兼容问题. 如果是遵循标准的,则 new Set([,-0]))[1]
返回 0
, 否则会返回 -0
, 1 / -0
的结果为 -Infinity
不等于 Infinity, 此时使用空对象来作为标准 Set
的替代实现,
添加回答
举报
0/150
提交
取消