Math.max([])为0
并且是 []
但是为什么Math.max(… [])在ES2015中等于-Infinity?
解决方法
Math.max([])会发生什么,首先将[]转换为字符串然后转换为数字.它实际上并不被认为是一个参数数组.
使用Math.max(… []),数组被视为通过扩展运算符的参数集合.由于数组为空,这与不带参数的调用相同.
根据docs产生的 – 无穷大
If no arguments are given,the result is -Infinity.
console.log(+[]); //0 [] -> '' -> 0 console.log(+[3]); //3 [] -> '3' -> 3 console.log(+[3,4]); //Nan console.log(...[3]); //3 console.log(...[3,4]); //3 4 (the array is used as arguments) console.log(Math.max([])); //0 [] is converted to 0 console.log(Math.max()); // -infinity: default without arguments console.log(Math.max(...[])); // -infinity console.log(Math.max([3,4])); //Nan console.log(Math.max(...[3,4])); //4