franklee
franklee

Reputation: 63

use of Javascript math.max.apply(null or math..)

First example

var arr1 = ['a', 'b'];
var arr2 = ['c', 'd'];

arr1.push.apply(arr1, arr2);
    // arr1 is now ['a', 'b', 'c', 'd']

second example

Math.max.apply(null, [-1, 5, 11, 3])

In the first one above, can we just use array1.push(array2), is there any different?

In the second one above, the null param really confusing to me and what's more, it seems that max mehood need number type, not an array?

Upvotes: 0

Views: 2450

Answers (4)

Deschutron
Deschutron

Reputation: 11

f.apply(x, y) is x.f(the contents of y).

Where f is arr1.push, x is arr1, and y is arr2, that's arr1.push.apply(arr1, arr2) is arr1.push(the contents of arr2).

Note, arr1.push adds all its arguments to arr1, so arr1.push(arr2) adds arr2 to arr1, making it ['a', 'b', ['c', 'd']].

Where f is Math.max, and y is [-1, 5, 11, 3], that's Math.max.apply(x, [-1, 5, 11, 3]).

But what do you put in x?

Math.max is made to work as function called on its own, not as a method of an object.

If you leave out x and do Math.max.apply([-1, 5, 11, 3]), Math.max will think it's being called as a method of the array and without any arguments. So it will give the result of Math.max().

So then you have to put something in the x, and it doesn't matter what, because Math.max is not made to use it, so it will ignore it.

The best value you can give x is the simplest, which is null.

If you're wondering what a line of Javascript code will do, it's a good idea to test it.

Firefox and Chrome both have a built-in Javascript command-line you can use, and there are websites that do Javascript command-lines too. I used Firefox > Tools > Web Developer > Web Console to check my answer.

Upvotes: 1

Barmar
Barmar

Reputation: 781706

If you use arr1.push(arr2), the result is different:

['a', 'b', ['c', 'd']]

To get the same result as your original arr1.push.apply, you would have to do

arr1.concat(arr2);

apply takes the array argument and turns each element into a separate argument to the function being called. When you call the function normally, the array is just a single argument.

The normal way to call Math.max is with each number that it's comparing as a separate argument:

Math.max(-1, 5, 11, 3);

In order to get the arguments from an array, you have to use apply to spread them.

The reason for the null argument to in the second case is because the first argument to apply is the context of the function, i.e. the value that will be assigned to this inside the function. When you call a function normally, the context is the value before the .; for example, when you write

arr1.push('x');

the context is the arr1 array. Math.max() doesn't use its context, but you have to pass something, so null is a common placeholder. But when we call push using apply, we have to pass arr1 as the context, so it knows which array to modify. Some people like to write

Math.max.apply(Math, [-1, 5, 11, 3]);

so that the context will be the same as if you'd called Math.max in the normal way. But it doesn't matter in this case.

Upvotes: 4

Patrick Motard
Patrick Motard

Reputation: 2660

first example

array1.push(array2)

array1 becomes ['a', 'b', ['c', 'd']]

second example

See thefoureye's explanation here

Namely:

There is another advantage, of using apply, you can choose your own context. The first parameter, you pass to apply of any function, will be the this inside that function. But, max doesn't depend on the current context. So, anything would work in-place of Math.

Upvotes: 0

laurent
laurent

Reputation: 2620

  1. you will add the second array as a value of the first ([a,b,[c,d]]). If you want to merge both array I suggest you to call .concat instead

  2. The first argument in "apply" and "call" defines the context used when the function is called: the "this". For math.max the context is not important and therefore the context can be null, you can pass Math as well which in my opinion would be more semantically correct. But it does not matter in this case

Upvotes: 0

Related Questions