0

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?

franklee
  • 63
  • 11
  • 1. no. that would put a reference to that array in `arr1`. 2. math.max can take any number - this just allows you to call it with an array. – Daniel A. White Jan 29 '16 at 02:33

4 Answers4

3

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • so why we can use just Math.max(-1, 5, 11, 3) rather than Math.max.apply(Math, [-1, 5, 11, 3]). what the point of using Math.max.apply(Math, [-1, 5, 11, 3])? – franklee Feb 01 '16 at 05:35
  • You wouldn't use it with a literal array, you would use it when you have the array in a variable that you filled in dynamically. Your code is just an example. – Barmar Feb 01 '16 at 05:41
  • cool,I am wondering why you are saying that 'apply takes the array argument and turns each element into a separate argument to the function being called'? – franklee Feb 01 '16 at 05:43
  • `someFunction.apply(null, [1, 2, 3])` is equivalent to `someFunction(1, 2, 3)`. Each element of the array becomes a separate argument to the function. – Barmar Feb 01 '16 at 05:46
  • Okay, really appreciate for yr time – franklee Feb 01 '16 at 05:52
1

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.

Deschutron
  • 11
  • 1
0
  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

laurent
  • 2,590
  • 18
  • 26
  • And can we just use array1.push(array2) and why the second parameter is the array in the max method? – franklee Jan 29 '16 at 02:42
  • array.apply(array,[1,2,3,4]) is equivalent to array.push(1,2,3,4) (see partial application) which is different than array.push([1,2,3,4]) The second argument in Math.max.apply are the arguments you want to partially apply, it will be equivalent to Math.max(-1,5,11,3). I think the key word you need to search is partial application – laurent Jan 29 '16 at 02:48
0

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.

Community
  • 1
  • 1
Patrick Motard
  • 2,650
  • 2
  • 14
  • 23