0

In

var values = [1,2,3,4,5];
var max = Math.max.apply(Math, values);

Why do we have to pass in Math as the this object? I have a hard time understanding this.

Is there anything specifically that apply needs this(Math) for in its execution?

Sahand
  • 7,980
  • 23
  • 69
  • 137
  • 3
    No, and you don’t have to pass it. `Math.max.apply(null, values)` will work fine. – Ry- Sep 22 '17 at 14:03
  • Since the method doesn't use `this` internally, actually you can pass anything there. – Wiktor Zychla Sep 22 '17 at 14:04
  • In this case you don't need to, since `Math.max` does need `this`, so even `null` will also work. – gurvinder372 Sep 22 '17 at 14:05
  • Interesting, because in Nicholas Zakas' book on Javascript he writes that we need to pass it: "The key to this technique is to pass in the Math object as the first argument of apply() so that the this value is set appropriately. Then you can pass an array in as the second argument." Maybe this is dated information since the book is from 2012. – Sahand Sep 22 '17 at 14:05
  • 1
    @Sandi: It guarantees that the call is exactly equivalent to `Math.max(1, 2, 3, 4, 5)` (since that call has an object of `Math`, like any other `x.y()` call), but I can’t imagine any implementation choosing to break on `var max = Math.max; max(1, 2, 3, 4, 5)`. It should be forbidden to break by the standard (that’s how I would read it – there’s no specific note about it, but nothing references `this` values, and “it didn’t say *not* to break while following the standard steps” doesn’t sound very standards-compliant). – Ry- Sep 22 '17 at 14:11

2 Answers2

1

The apply() method (Function.prototype.apply()) allows you to pass arguments as an array to a function as well as the context through this.

func.appy(this, [argumentarray])

In this case, max doesn't need or use the current context so you could put anything and it will work, including null.

See this question: How does the Math.max.apply() work?

Sahand
  • 7,980
  • 23
  • 69
  • 137
0

It depends on the internal implementation details of max. If it uses this at all and expects this to be Math, then it is required. If it doesn't use this in any way, it's pretty irrelevant what you pass as thisArg.

max may arguably use some helper methods of Math for its internal implementation. Unless you specifically know that it doesn't, you should preserve its context. (Generally speaking for any time you use apply or call or pass a method around.)

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Okay, so if I need to do it, why is `Math` the appropriate context here rather than for example `global`? – Sahand Sep 22 '17 at 14:08
  • 1
    If you call it regularly via `Math.max()`, the context is `Math` by virtue of the way you called it. So if `max` expects anything as its context, it would be `Math`. – deceze Sep 22 '17 at 14:10
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/155094/discussion-on-answer-by-deceze-why-do-we-have-to-pass-this-to-apply). – deceze Sep 22 '17 at 14:25