网站首页 > 知识剖析 正文
内置函数Math.max()和Math.min()可以分别找出参数中的最大值和最小值。
Math.max(1, 2, 3, 4); // 4
Math.min(1, 2, 3, 4); // 1
这些函数对于数字组成的数组是不能用的。但是,这有一些类似的方法。
Function.prototype.apply()让你可以使用提供的this与参数组成的_数组(array)_来调用函数。
var numbers = [1, 2, 3, 4];
Math.max.apply(null, numbers) // 4
Math.min.apply(null, numbers) // 1
给apply()第二个参数传递numbers数组,等于使用数组中的所有值作为函数的参数。
一个更简单的,基于ES2015的方法来实现此功能,是使用展开运算符.
var numbers = [1, 2, 3, 4];
Math.max(...numbers) // 4
Math.min(...numbers) // 1
此运算符使数组中的值在函数调用的位置展开。
猜你喜欢
- 2025-06-10 LeetCode 力扣官方题解 | 521. 最长特殊序列Ⅰ
- 2025-06-10 进一步理解函数(进一步理解函数的方法)
- 2025-06-10 二维码能跑游戏?开发者 3KB 复刻 DOOM 崩溃,ChatGPT 救场
- 2025-06-10 数据结构:布隆过滤器(Bloom Filter)
- 2025-06-10 数据质量动态探查及相关前端实现(数据质量检查方法)
- 2025-06-10 HarmonyOS实战:实现任意拖动的应用悬浮窗口
- 2025-06-10 一文搞懂堆外内存(模拟内存泄漏)(堆内存 堆外内存)
- 2025-06-10 JavaScript展开运算符与剩余参数,灵活操作数组与对象的终极利器
- 2025-06-10 为什么 MapReduce 再次流行起来了?
- 2025-06-10 java Math类和Random类的用法(java中的random怎么用)
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)