简体   繁体   English

JavaScript Math(参数传递,数组和“apply”方法)

[英]JavaScript Math (parameter passing, arrays, and the “apply” method)

1) Why does the function smallest fail? 1)为什么function smallest失败? I think it conforms to the example in the Mozilla documentation https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/min , which says 我认为它符合Mozilla文档https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/min中的示例,

 function getMin(x,y) {  //from Mozilla documentation
   return Math.min(x,y)
}



function smallest(array){ //my own experimentation with Resig`s example
  return Math.min(array);
}
function largest(array){      //from John Resig`s learning advanced JavaScript #41
  return Math.max.apply( Math, array );
}
assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");

The "Math.min()" function can take an arbitrary number of arguments. “Math.min()”函数可以使用任意数量的参数。 However, passing a single array to the function is just passing in one argument, not several. 但是,将单个数组传递给函数只是传入一个参数,而不是几个。 That single argument is the array itself. 那个单一的参数就是数组本身。 Arrays, you will recall, are first-rate values. 你会记得,数组是一流的价值观。

That's the whole point of the "apply" function that's available on every function. 这就是每个功能都可以使用的“应用”功能的全部要点。 It is used to invoke the function and pass it an argument list composed of entries in an array instance. 它用于调用函数并将其传递给由数组实例中的条目组成的参数列表。 When you do this: 当你这样做:

var array = [ 1, 2, 3, 4, 5 ];
someFunction.apply(irrelevant, array);

that's like calling: 这就像打电话:

someFunction(1, 2, 3, 4, 5);

(The "irrelevant" argument is what's to be used as the this value in the function call.) (“不相关”参数是在函数调用中用作this值的内容。)

Math.min and Math.max takes any number of numbers, as arguments. Math.minMath.max接受任意数量的数字作为参数。 Your smallest is trying to pass an array, not numbers. smallest是尝试传递数组,而不是数字。

The use of apply (as in largest in your example) pastes the elements of the array as arguments. apply的使用(如示例中的largest )将数组的元素粘贴为参数。

It doesn't conform to the example. 它不符合这个例子。 The []'s in the documentation are optional indicators, not array delimiters. 文档中的[]是可选指标,而不是数组分隔符。

Try 尝试

function smallest(array) { return Math.min.apply(Math, array); }

It won't work when passed as an array because min converts its arguments to numbers before comparing. 它作为数组传递时不起作用,因为min在比较之前将其参数转换为数字。 Trying to convert an array to a number will, unless the array contains a single element that looks like a number, produce NaN . 尝试将数组转换为数字,除非数组包含看起来像数字的单个元素,否则会生成NaN

So 所以

Math.min([3]) === 3
Math.min('3') === 3

but

isNaN(Math.min([3, 4]))  // because,
isNaN(Math.min('3,4'))  // which is the same as
isNaN(Math.min(+'3,4'))  // which is equivalent to
isNaN(Math.min(NaN))

.apply( object, array ) is the correct syntax to unpack an array into an argument list. .apply( object, array )是将数组解压缩到参数列表中的正确语法。 Just passing an array to a function that converts all arguments to a number isn't useful, since it will only see NaN. 只是将数组传递给将所有参数转换为数字的函数是没用的,因为它只会看到NaN。

apply passes the array to Math.min as an arguments object- Math.min(a,b,c,d,e) apply将数组传递给Math.min作为参数对象 - Math.min(a,b,c,d,e)

smallest is passing the whole array as the first agument- 最小的是通过整个阵列作为第一个文章 -

Math.min(x) and it really has nothing to compare it to. Math.min(x)并没有什么可比的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM