Math.min.apply(0,array) - 为什么? [英] Math.min.apply(0, array) - why?

查看:240
本文介绍了Math.min.apply(0,array) - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Math.min.apply(0, x)

其中 x 是一个数组。为什么你会这样做?行为似乎是从数组中获取最小值 x

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

推荐答案

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

I realized the answer as I was posting my own question: This is the most succinct way of taking the min of an array x in 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.

参见 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); };

这篇关于Math.min.apply(0,array) - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆