gpt4 book ai didi

javascript - 在 Javascript 中,为什么空数组的最小值是无穷大?

转载 作者:搜寻专家 更新时间:2023-11-01 04:56:14 24 4
gpt4 key购买 nike

我正在使用生成器创建对象,如下所示:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com