欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
JavaScript 在数组上模拟 max 方法
 
您可以使用 Math.max() 方法找到(数字列表中的)最大数字:
 
实例
 
Math.max(1,2,3);  // 会返回 3
 
由于 JavaScript 数组没有 max() 方法,因此您可以应用 Math.max() 方法。
 
实例
 
Math.max.apply(null, [1,2,3]); // 也会返回 3
 
第一个参数(null)无关紧要。在本例中未使用它。
 
这些例子会给出相同的结果:
 
实例
 
Math.max.apply(Math, [1,2,3]); // 也会返回 3
 
实例
 
Math.max.apply(" ", [1,2,3]); // 也会返回 3
 
实例
 
Math.max.apply(0, [1,2,3]); // 也会返回 3

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64474.shtml