Javascript Math.max和Math.min实际上是如何工作的?

Javascript Math.max和Math.min实际上是如何工作的?,javascript,math,Javascript,Math,我真的很好奇这些函数实际上是如何工作的?我知道有很多关于如何使用这些的问题,我已经知道如何使用它们,但是我找不到任何地方如何在阵列上实际实现这些功能,例如,如果没有这样的功能?如果没有助手,您将如何编写这样一个函数 基本功能: Math.max()和Math.min()用于数字(或它们可以强制转换为数字的内容),不能直接将数组作为参数传递 例: 可以有多个逗号分隔的数字 数组: 此示例显示了如何将它们应用于阵列: 基本上以下工作: Math.max.apply(null, [1,5,2,3])

我真的很好奇这些函数实际上是如何工作的?我知道有很多关于如何使用这些的问题,我已经知道如何使用它们,但是我找不到任何地方如何在阵列上实际实现这些功能,例如,如果没有这样的功能?如果没有助手,您将如何编写这样一个函数

基本功能:

Math.max()和Math.min()用于数字(或它们可以强制转换为数字的内容),不能直接将数组作为参数传递

例:

可以有多个逗号分隔的数字

数组:

此示例显示了如何将它们应用于阵列:

基本上以下工作:

Math.max.apply(null, [1,5,2,3]);
为什么这样做?

这是因为apply是一个所有函数都具有的函数,它应用一个具有数组参数的函数


Math.max.apply(null[1,5,2,3])与Math.max(1,5,2,3)相同。

这是Chrome V8引擎中的Math.max代码

function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER(arg1);
    arg2 = TO_NUMBER(arg2);
    if (arg2 > arg1) return arg2;
    if (arg1 > arg2) return arg1;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1;
    }
    // All comparisons failed, one of the arguments must be NaN.
    return NaN;
  }
  var r = -INFINITY;
  for (var i = 0; i < length; i++) {
    var n = %_Arguments(i);
    n = TO_NUMBER(n);
    // Make sure +0 is considered greater than -0.
    if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) {
      r = n;
    }
  }
  return r;
}
函数MathMax(arg1,arg2){//length==2
变量长度=%\u参数长度();
如果(长度==2){
arg1=TO_编号(arg1);
arg2=TO_编号(arg2);
如果(arg2>arg1)返回arg2;
如果(arg1>arg2)返回arg1;
如果(arg1==arg2){
//确保-0被认为小于+0。
返回(arg1==0&%\u IsMinusZero(arg1))?arg2:arg1;
}
//所有比较均失败,其中一个参数必须为NaN。
返回NaN;
}
var r=-无穷大;
对于(变量i=0;ir | | |(r==0&&n==0&&n%| IsMinusZero(r))){
r=n;
}
}
返回r;
}

是存储库。

这很容易用
数组实现。原型。reduce

function min() {
   var args = Array.prototype.slice.call(arguments);

   var minValue = args.reduce(function(currentMin, nextNum) {
      if (nextNum < currentMin) {
         // nextNum is less than currentMin, so we return num
         // which will replace currentMin with nextNum
         return nextNum;
      }
      else {
         return currentMin;
      }
   }, Infinity);

   return minValue;
}
函数min(){
var args=Array.prototype.slice.call(参数);
var minValue=args.reduce(函数(currentMin,nextNum){
if(nextNum
下面是在不存在
Math.min()
Math.max()
的情况下如何实现这些函数

函数有一个对象,您可以遍历该对象以获取其值

需要注意的是,不带参数的返回
无穷大,而不带参数的返回
-无穷大

函数min(){
var结果=无穷大;
for(参数中的变量i){
if(参数[i]<结果){
结果=参数[i];
}
}
返回结果;
}
函数max(){
var结果=-无穷大;
for(参数中的变量i){
if(参数[i]>结果){
结果=参数[i];
}
}
返回结果;
}
//测验
控制台日志(最小值(5,3,-2,4,14))//-2.
日志(数学最小值(5,3,-2,4,14))//-2.
控制台日志(最大值(5,3,-2,4,14))//14
log(数学最大值(5,3,-2,4,14))//14
log(min())//无穷
log(Math.min())//无穷
log(max())//-无穷

log(Math.max())//-无穷大
好吧,这里是
min
,没有
Math.min
(代码在ES6中)


Max
可以用几乎相同的逻辑来实现,只是您要跟踪到目前为止看到的最高值,初始值是
-Infinity

让我们看看规范(它可以/应该帮助您实现!)

在(Math.max/min的初始定义)中,我们看到以下内容:

15.8.2.11 max(x, y)
Returns the larger of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x>y, the result is x.
    • If y>x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is +0.
    • If x is −0 and y is +0, the result is +0.
    • If x is −0 and y is −0, the result is −0.

15.8.2.12 min(x, y)
    Returns the smaller of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x<y, the result is x.
    • If y<x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is −0.
    • If x is −0 and y is +0, the result is −0.
    • If x is −0 and y is −0, the result is −0.
11.8.5的参考可在此处找到:


同样,7.2.11可以在这里找到:

初始化一个值;迭代集合,测试每个元素是否大于或小于当前最高/最低值;如果是这样,用该元素替换该值。别忘了,它也适用于与数组具有相同属性的对象:
Math.max.apply(null,{0:1,1:4,2:2,length:3})
@ahitt6345这是一个很好的观点;这可能会有帮助。当然你必须做Math.functionname.apply(null,obj);我只是想指出,万一有人想知道为什么复制不起作用。哎呀,忘了我想应用的函数了。。。谢谢你告诉我lolMath.max()和Math.min()用于数字。在ES6中,您可以使用扩展运算符
Math.max(…array)
,但请注意,参数的数量是最大的:您不需要先切片;您只需执行
Array.prototype.reduce.call即可。
function min() {
  return Array.from(arguments).reduce(
    (minSoFar, next) => minSoFar < next ? minSoFar : next
  , Infinity)
}
function min() {
  let minSoFar = Infinity
  for(let i = 0, l = arguments.length; i < l; i++) {
    const next = arguments[i]
    minSoFar = minSoFar < next ? minSoFar : next
  }
  return minSoFar
}
15.8.2.11 max(x, y)
Returns the larger of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x>y, the result is x.
    • If y>x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is +0.
    • If x is −0 and y is +0, the result is +0.
    • If x is −0 and y is −0, the result is −0.

15.8.2.12 min(x, y)
    Returns the smaller of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x<y, the result is x.
    • If y<x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is −0.
    • If x is −0 and y is +0, the result is −0.
    • If x is −0 and y is −0, the result is −0.
15.8.2.11 max ( [ value1 [ , value2 [ , … ] ] ] )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

    • If no arguments are given, the result is −∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the largest value is done as in 11.8.5 except that +0 is considered to be larger than −0.

    The length property of the max method is 2.

15.8.2.12 min ( [ value1 [ , value2 [ , … ] ] ] )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.

    • If no arguments are given, the result is +∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the smallest value is done as in 11.8.5 except that +0 is considered to be larger than −0.

    The length property of the min method is 2.
20.2.2.24 Math.max ( value1, value2 , …values )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

    • If no arguments are given, the result is −∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0.

    The length property of the max method is 2.

20.2.2.25 Math.min ( value1, value2 , …values )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.

    • If no arguments are given, the result is +∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0.

The length property of the min method is 2.