Math.max.apply()如何工作?

Sha*_*ane 73 javascript function apply

Math.max.apply()工作怎么样?

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <script>
      var list = ["12","23","100","34","56",
                                    "9","233"];
      console.log(Math.max.apply(Math,list));    
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

上面的代码在List中找到Max number.任何人都可以告诉我下面的代码是如何工作的?如果我通过它似乎有效null or Math.

console.log(Math.max.apply(Math,list));
Run Code Online (Sandbox Code Playgroud)

是否所有user-defined/Native functions有可以使用的电话和申请方法?

the*_*eye 118

apply接受一个数组,它将数组作为参数应用于实际函数.所以,

Math.max.apply(Math, list);
Run Code Online (Sandbox Code Playgroud)

可以理解为,

Math.max("12", "23", "100", "34", "56", "9", "233");
Run Code Online (Sandbox Code Playgroud)

因此,apply将数据数组作为参数传递给函数是一种方便的方法.记得

console.log(Math.max(list));   # NaN
Run Code Online (Sandbox Code Playgroud)

将无法工作,因为max不接受数组作为输入.

使用时还有另一个优点,apply您可以选择自己的上下文.您传递给apply任何函数的第一个参数将是该this函数的内部.但是,max不依赖于当前的背景.所以,任何事情都可以取而代之Math.

console.log(Math.max.apply(undefined, list));   # 233
console.log(Math.max.apply(null, list));        # 233
console.log(Math.max.apply(Math, list));        # 233
Run Code Online (Sandbox Code Playgroud)

由于apply实际上是定义的Function.prototype,所以任何有效的JavaScript函数对象都将具有apply默认的函数.

  • @Shane因为,即使我们将上下文设置为`undefined`或`null` :)它也可以工作.如果`max`试图访问/更改上下文中的任何内容,当上下文设置为`undefined`或`时,它就会失败null`. (3认同)
  • @NiCkNewman 实际上,`Math.max` 不会使用第一个参数,因为它不需要任何上下文。它只对它在参数中接收到的数据进行操作。 (2认同)
  • .apply()的最佳解释! (2认同)
  • 为什么 null 或 math ? (2认同)

Wal*_*anG 28

在JavaScript ES6上只使用Spread运算符:

var list = ["12","23","100","34","56","9","233"];
console.log(Math.max(...list));
//                   ^^^ Spread operator 
Run Code Online (Sandbox Code Playgroud)

  • 要检查“Spread operator”兼容性:http://kangax.github.io/compat-table/es6/#test-spread_(...)_operator (3认同)

zer*_*kms 17

谁能告诉我下面的代码是如何工作的?

Math.max.apply(Math,list)
Run Code Online (Sandbox Code Playgroud)

调用一个Math.max带有Mathobject 的函数,用作this函数实现(body)中的引用,并list作为参数传递.

所以这最终等于

Math.max("12","23","100","34","56", "9","233")
Run Code Online (Sandbox Code Playgroud)

如果我传递null或Math,它似​​乎有效.

显然,Math.max实现不使用实例变量 - 没有理由这样做.天真的实现只会迭代arguments并找到最大的实现.

是否所有用户定义/ Native函数都有我们可以使用的调用和应用方法?

是的,可以使用call或调用每个函数apply

参考文献:


小智 6

我首先要说的是Math.max(...numbers), andFunction.prototype.apply()应该只用于元素相对较少的数组。如果数组太大, (...) 和 apply 将失败或返回错误结果

Math.max.apply(null | undefined | Math, numbers)是一样的,所以出于审美原因Math.max(...numbers)我会推荐。Math.max(...numbers)

const numbers = [5, 6, 2, 3, 7];

const max = Math.max(...numbers);

console.log('max:', max);
// expected output: 7

const min = Math.min(...numbers);

console.log('min:', min);
// expected output: 2
Run Code Online (Sandbox Code Playgroud)

如果您需要查找非常大的数值数组中的最大元素:请使用该Array.reduce()方法。

Array.reduce()可用于通过比较每个值来查找数值数组中的最大元素:

const numbers = [5, 6, 2, 3, 7];
const getMax = (numbers) => numbers.reduce((a, b) => Math.max(a, b));

const getMin = (numbers) => numbers.reduce((a, b) => Math.min(a, b));
	
const max = getMax(numbers)
const min = getMin(numbers)

console.log('max:', max)
console.log('min:', min)
Run Code Online (Sandbox Code Playgroud)

结论:

数值数组比较小:使用Math.max(...numbers) 数值数组非常大:使用Array.reduce()方法

  • JavaScriptCore 引擎的硬编码参数限制为 65536,因此在使用 apply() 时,您应该确保数组大小始终较小。 (2认同)