Javascript Math.min.apply(0, array) - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2870015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:16:34  来源:igfitidea点击:

Math.min.apply(0, array) - why?

javascript

提问by Trevor Burnham

I was just digging through some JavaScript code (Rapha?l.js) and came across the following line (translated slightly):

我只是在挖掘一些 JavaScript 代码 (Rapha?l.js) 并遇到以下行(略有翻译):

Math.min.apply(0, x)

where xis an array. Why on earth would you do this? The behavior seems to be "take the min from the array x."

哪里x是一个数组。你到底为什么要这样做?行为似乎是“从数组中取出 min x”。

回答by Trevor Burnham

I realized the answer as I was posting my own question: This isthe most succinct way of taking the min of an array xin JavaScript. The first argument is totally arbitrary; I find the 0 confusing because the code intuitively means "Take the min of 0 and x," which is absolutely not the case. Using the Math object makes more sense for human-readability, but the Raphael.js authors are obsessed with minification and 0 is three bytes shorter.

我在发布自己的问题时意识到了答案:这x在 JavaScript中获取数组最小值的最简洁方法。第一个论点完全是武断的;我发现 0 令人困惑,因为代码直观地表示“取 0 和 x 的最小值”,这绝对不是这种情况。使用 Math 对象对人类可读性更有意义,但 Raphael.js 作者痴迷于缩小,而 0 更短三个字节。

See http://ejohn.org/blog/fast-javascript-maxmin/

http://ejohn.org/blog/fast-javascript-maxmin/

For readability's sake, I'd strongly urge people to stop doing this and instead define a function along the lines of

为了可读性,我强烈敦促人们停止这样做,而是按照以下方式定义一个函数

function arrayMin(arr) { return Math.min.apply(Math, arr); };

回答by David Rosson

The reason is this:

原因是这样的:

  • Your input xis an array

  • The signature of Math.min()doesn't take arrays, only comma separated arguments

  • If you were using Function.prototype.call()it would have almost the same signature, except the first argument is the thiscontext, i.e.who's "calling"

    • Example: Math.min.call(context, num1, num2, num3)
  • The contextonly matters when you refer to thisinside the function, e.g.most methods you can call on an array: Array.prototype.<method>would refer to this(the array to the left of the 'dot') inside the method.

  • Function.prototype.apply()is very similar to .call(), only that instead of taking comma-separated arguments, it now takes an array after the context.

    • Function.prototype.call(context, arg1, arg2, arg3)
    • Function.prototype.apply(context, [arg1, arg2, arg3])
  • The 0or nullthat you put in as the first argument is just a place shifter.

  • 您的输入x是一个数组

  • 的签名Math.min()不接受数组,只接受逗号分隔的参数

  • 如果您正在使用Function.prototype.call()它,它将具有几乎相同的签名,除了第一个参数是this上下文,谁在“调用”

    • 例子: Math.min.call(context, num1, num2, num3)
  • context只有当你指的事项this里面的函数,例如,你可以在阵列上调用大多数方法:Array.prototype.<method>将引用this(数组左边的“点”)的方法内。

  • Function.prototype.apply()与 非常相似.call(),只是它现在不是采用逗号分隔的参数,而是在context.

    • Function.prototype.call(context, arg1, arg2, arg3)
    • Function.prototype.apply(context, [arg1, arg2, arg3])
  • 您作为第一个参数放入的0ornull只是一个位置移位器。

回答by Igor Vaschuk

The proper and less confusing way to call this is: Math.min.apply(null, array)

调用它的正确且不那么令人困惑的方法是: Math.min.apply(null, array)

If parameter is unused setting it to null makes code more readable than setting it to 0 or Math

如果参数未使用,则将其设置为 null 会使代码比将其设置为 0 或 Math 更具可读性

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

回答by Mike Q

Here is an example where we can get the MAX and MIN timezone offsets for the given year for a timezone. It returns the two changes for the year to account for DST and ST. (i.e. ST = 300 an DST = 360 for the timezone in question).

这是一个示例,我们可以获取给定年份的时区的 MAX 和 MIN 时区偏移量。它返回当年的两个变化以说明 DST 和 ST。(即 ST = 300 和 DST = 360 对于相关时区)。

   var arr = [];
   for (var i = 0; i < 365; i++) {
       var d = new Date();
       d.setDate(i);
       var newoffset = d.getTimezoneOffset();
       arr.push(newoffset);
   }
   var DST = Math.min.apply(null, arr);
   var nonDST = Math.max.apply(null, arr);
   tw.local.stInt = DST
   tw.local.edInt = nonDST