javascript - javascript控制台-infinity,这是什么意思?
问题描述
我正在学习 javascript ES6,当我运行此代码时,我刚刚在控制台上发现了 -infinity:
let numeros = [1, 5, 10, 20, 100, 234];
let max = Math.max.apply(numeros);
console.log(max);
这是什么意思?
问候
解决方案
的第一个参数Function#apply
是 thisArg ,您只是将thisArg
数组作为数组传递,这意味着它在Math#max
没有任何参数的情况下调用。
根据MDN 文档:
如果没有给出参数,则结果为 -Infinity。
为了解决您的问题集Math
或null
作为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);
推荐阅读
- html - Create multiple tables based on json data in Angular 8
- r - 如何在R中将两列合并为一列?
- c++ - How to kill/signal other child processes from another child process?
- python - 在 for 循环中调用和演化函数
- reactjs - Error with Next Js with Expo when compiling
- python - Pandas pd.merge gives nan
- android - Firestore whereIn 查询文档是随机排序的
- c++ - 将公共成员函数指针传递给构造函数
- javascript - html输入框的“value”属性内的if语句
- android - Google Android TTS human readable names of voices