首页 > 解决方案 > javascript控制台-infinity,这是什么意思?

问题描述

我正在学习 javascript ES6,当我运行此代码时,我刚刚在控制台上发现了 -infinity:

let numeros = [1, 5, 10, 20, 100, 234];
let max = Math.max.apply(numeros);
console.log(max);

这是什么意思?

问候

标签: javascriptecmascript-6console

解决方案


的第一个参数Function#apply是 thisArg ,您只是将thisArg数组作为数组传递,这意味着它在Math#max没有任何参数的情况下调用。

根据MDN 文档:

如果没有给出参数,则结果为 -Infinity。

为了解决您的问题集Mathnull作为thisArg

let max= Math.max.apply(Math, numeros );

let numeros= [1,5,10,20,100,234];
    let max= Math.max.apply(Math, numeros );
    
    console.log( max );


正如@FelixKling建议的那样,从 ES6 开始,您可以使用扩展语法来提供参数。

Math.max(...numeros)

let numeros = [1, 5, 10, 20, 100, 234];
let max = Math.max(...numeros);

console.log(max);


推荐阅读