我真的很好奇这些功能是如何运作的?我知道有很多关于如何使用它们的问题,我已经知道如何使用它们,但我无法在任何地方找到如何实际在数组上实现此功能,例如,如果没有这样的功能?如果没有帮助者,你会如何编写这样的函数?
这是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;
}
Run Code Online (Sandbox Code Playgroud)
这是存储库.
下面是如何实现的功能,如果Math.min()
和Math.max()
根本不存在的。
函数有一个arguments
对象,你可以遍历它来获取它的值。
重要的是要注意,Math.min()
不带参数返回Infinity
,Math.max()
不带参数返回-Infinity
。
function min() {
var result= Infinity;
for(var i in arguments) {
if(arguments[i] < result) {
result = arguments[i];
}
}
return result;
}
function max() {
var result= -Infinity;
for(var i in arguments) {
if(arguments[i] > result) {
result = arguments[i];
}
}
return result;
}
//Tests
console.log(min(5,3,-2,4,14)); //-2
console.log(Math.min(5,3,-2,4,14)); //-2
console.log(max(5,3,-2,4,14)); //14
console.log(Math.max(5,3,-2,4,14)); //14
console.log(min()); //Infinity
console.log(Math.min()); //Infinity
console.log(max()); //-Infinity
console.log(Math.max()); //-Infinity
Run Code Online (Sandbox Code Playgroud)
让我们看一下规范(它可以/应该帮助您实施!)
\n\n在ECMAScript 第一版 (ECMA-262)(Math.max/min 的初始定义)中,我们看到以下内容:
\n\n15.8.2.11 max(x, y)\nReturns the larger of the two arguments.\n \xe2\x80\xa2 If either argument is NaN, the result is NaN.\n \xe2\x80\xa2 If x>y, the result is x.\n \xe2\x80\xa2 If y>x, the result is y.\n \xe2\x80\xa2 If x is +0 and y is +0, the result is +0.\n \xe2\x80\xa2 If x is +0 and y is \xe2\x88\x920, the result is +0.\n \xe2\x80\xa2 If x is \xe2\x88\x920 and y is +0, the result is +0.\n \xe2\x80\xa2 If x is \xe2\x88\x920 and y is \xe2\x88\x920, the result is \xe2\x88\x920.\n\n15.8.2.12 min(x, y)\n Returns the smaller of the two arguments.\n \xe2\x80\xa2 If either argument is NaN, the result is NaN.\n \xe2\x80\xa2 If x<y, the result is x.\n \xe2\x80\xa2 If y<x, the result is y.\n \xe2\x80\xa2 If x is +0 and y is +0, the result is +0.\n \xe2\x80\xa2 If x is +0 and y is \xe2\x88\x920, the result is \xe2\x88\x920.\n \xe2\x80\xa2 If x is \xe2\x88\x920 and y is +0, the result is \xe2\x88\x920.\n \xe2\x80\xa2 If x is \xe2\x88\x920 and y is \xe2\x88\x920, the result is \xe2\x88\x920.\n
Run Code Online (Sandbox Code Playgroud)\n\n该规范的后续版本为我们提供了:
\n\n\n\n15.8.2.11 max ( [ value1 [ , value2 [ , \xe2\x80\xa6 ] ] ] )\n\n Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.\n\n \xe2\x80\xa2 If no arguments are given, the result is \xe2\x88\x92\xe2\x88\x9e.\n \xe2\x80\xa2 If any value is NaN, the result is NaN.\n \xe2\x80\xa2 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 \xe2\x88\x920.\n\n The length property of the max method is 2.\n\n15.8.2.12 min ( [ value1 [ , value2 [ , \xe2\x80\xa6 ] ] ] )\n\n Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.\n\n \xe2\x80\xa2 If no arguments are given, the result is +\xe2\x88\x9e.\n \xe2\x80\xa2 If any value is NaN, the result is NaN.\n \xe2\x80\xa2 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 \xe2\x88\x920.\n\n The length property of the min method is 2.\n
Run Code Online (Sandbox Code Playgroud)\n\n11.8.5的参考可以在这里找到:抽象关系比较算法
\n\n\n\n20.2.2.24 Math.max ( value1, value2 , \xe2\x80\xa6values )\n\n Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.\n\n \xe2\x80\xa2 If no arguments are given, the result is \xe2\x88\x92\xe2\x88\x9e.\n \xe2\x80\xa2 If any value is NaN, the result is NaN.\n \xe2\x80\xa2 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 \xe2\x88\x920.\n\n The length property of the max method is 2.\n\n20.2.2.25 Math.min ( value1, value2 , \xe2\x80\xa6values )\n\n Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.\n\n \xe2\x80\xa2 If no arguments are given, the result is +\xe2\x88\x9e.\n \xe2\x80\xa2 If any value is NaN, the result is NaN.\n \xe2\x80\xa2 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 \xe2\x88\x920.\n\nThe length property of the min method is 2.\n
Run Code Online (Sandbox Code Playgroud)\n\n同样,7.2.11 可以在这里找到:抽象关系比较
\n基本功能:
Math.max() 和 Math.min() 用于数字(或它们可以强制转换为数字的数字),您不能直接将数组作为参数传递。
前任:
Math.max(1,52,28)
Run Code Online (Sandbox Code Playgroud)
您可以有多个逗号分隔的数字。
数组:
此示例展示了如何将它们应用于数组:
基本上有以下几个作品:
Math.max.apply(null, [1,5,2,3]);
Run Code Online (Sandbox Code Playgroud)
为什么这样有效?
这是有效的,因为 apply 是所有函数都具有的函数,它将函数与数组的参数一起应用。
Math.max.apply(null, [1,5,2,3]) 与 Math.max(1,5,2,3) 相同
归档时间: |
|
查看次数: |
5110 次 |
最近记录: |