x >= JavaScript 中的 x 模式
x >= x pattern in JavaScript
在阅读 D3.js 的源代码时,我看到了 x >= x
模式。如果它是为了检测数字中的 NaN,为什么不只是 isNaN(x)
或 x == x
?
Source, where I encountered it:
d3.min = function(array, f) {
var i = -1, n = array.length, a, b;
if (arguments.length === 1) {
while (++i < n) if ((b = array[i]) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
} else {
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
}
return a;
};
好的,我看到 x >= x
对 NaN
和 undefined
给出了 false
。 (不同于 isNaN(x)
或 x == x
。)
编辑:虽然它是 x >= x
的用例之一,但在这种情况下(感谢@Felix Kling 指出了这一点)undefined
已经被检查。
根据我的调查,d3.min
应该适用于任何类型的可订购值,而不仅仅是数字。 isNaN
只能处理数字。
d3 实际上在某些时候使用了 ==
。 This commit 介绍了 x == x
测试:
Unlike Math.min
and Math.max
, it doesn't make sense to return negative or positive infinity for d3.min
and d3.max
; the D3 functions return the minimum value according to an arbitrary ordering, not by numeric value. Instead, the minimum or maximum of an empty array, or an array that contains only degenerate values, should always be undefined.
This commit 将 x == x
更改为 x <= x
(后来再次更改为 x >= x
):
In addition to NaN
, which is not equal to itself, you can have objects that are not orderable due to defined valueOf functions which return NaN. For example:
var o = new Number(NaN);
Here, o == o
is true, but o <= o
is false. Therefore it was possible for d3.min, d3.max and d3.extent to observe these non-orderable values rather than ignore them as intended. The fix is to check !(o <= o)
rather than o == o
.
在阅读 D3.js 的源代码时,我看到了 x >= x
模式。如果它是为了检测数字中的 NaN,为什么不只是 isNaN(x)
或 x == x
?
Source, where I encountered it:
d3.min = function(array, f) {
var i = -1, n = array.length, a, b;
if (arguments.length === 1) {
while (++i < n) if ((b = array[i]) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
} else {
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
}
return a;
};
好的,我看到 x >= x
对 NaN
和 undefined
给出了 false
。 (不同于 isNaN(x)
或 x == x
。)
编辑:虽然它是 x >= x
的用例之一,但在这种情况下(感谢@Felix Kling 指出了这一点)undefined
已经被检查。
根据我的调查,d3.min
应该适用于任何类型的可订购值,而不仅仅是数字。 isNaN
只能处理数字。
d3 实际上在某些时候使用了 ==
。 This commit 介绍了 x == x
测试:
Unlike
Math.min
andMath.max
, it doesn't make sense to return negative or positive infinity ford3.min
andd3.max
; the D3 functions return the minimum value according to an arbitrary ordering, not by numeric value. Instead, the minimum or maximum of an empty array, or an array that contains only degenerate values, should always be undefined.
This commit 将 x == x
更改为 x <= x
(后来再次更改为 x >= x
):
In addition to
NaN
, which is not equal to itself, you can have objects that are not orderable due to defined valueOf functions which return NaN. For example:var o = new Number(NaN);
Here,
o == o
is true, buto <= o
is false. Therefore it was possible for d3.min, d3.max and d3.extent to observe these non-orderable values rather than ignore them as intended. The fix is to check!(o <= o)
rather thano == o
.