我正在使用生成器创建对象,如下所示:
function* Thing() {
var x = 0;
while (x < 3) {
var rules = [{arr: [1, 2, 3]}, {arr: [1]}, {arr: []}];
yield {
arrayMinimum: Math.min(...rules[x].arr)
}
x++
}
}
var create = Thing();
console.log(create.next().value)
console.log(create.next().value)
console.log(create.next().value) // { arrayMinimum: Infinity } ???
为什么 Math.min(...[]) === Infinity
?
奖金混淆:Math.max(...[]) === -Infinity
???
最佳答案
这是因为 (...[])
指定的参数列表是空参数列表——也就是说,您正在执行 Math.min()
没有参数。
EMCAScript spec for Math.min
states :
If no arguments are given, the result is +∞.
(不出所料,它有 a similar statement for Math.max
。)
关于javascript - 在 Javascript 中,为什么空数组的最小值是无穷大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43053848/