Ad

Question About The Way How 'apply Method' Runs In Javascript

- 1 answer

I read documents which explain about apply ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply )

Although i read this whole document, I still don't understand the way how apply method runs

For example,

const string_array = ['a', 'b'];
const number_array = [0, 1, 2];
string_array.push.apply(string_array, number_array);
console.info(string_array); // ["a", "b", 0, 1, 2]

After calling string_array.push.apply, Why string_array returns ["a", "b", 0, 1, 2]?

Is that just a rule?. There is no reason???

Because string_array is a Array so, when string_array pushes number_array

Return value should be ["a", "b", [0, 1, 2] ]

I don't think the method of push work properly, when returns value is like ["a", "b", 0, 1, 2].

that is concat

Can anybody explain these why?

Ad

Answer

When you do this:

string_array.push(0, 1, 2);

...the push method receives this information (ignoring some details):

  • this = string_array, because when you call a function as part of a property access (string_array.push), JavaScript is defined such that this within the function call is the object you looked up the property on.
  • The arguments 0, 1, and 2, because you listed them explicitly when calling push.

When you do this:

string_array.push.apply(string_array, number_array)

you're specifying those same two pieces of information (what this is and what the arguments are), but you're doing it explicitly via the arguments to apply. The value for this is the first argument to apply, and the list of arguments is the second argument to apply, given as an array or array-like object (something with a length property and properties with number-like names).

That's really it. Either way, direct call or via apply, the JavaScript engine calls the push method with this set to string_array and the arguments 0, 1, and 2.

Why string_array.push.apply(string_array, number_array); returns ["a", "b", 0, 1, 2]?

It doesn't, it returns 5. The return value of push is the new length of the array. But after that call, string_array contains those entries, yes.

Return value should be ["a", "b", [0, 1, 2] ]

No, that's simply not how push and apply are defined. apply takes the array/array-like object you pass it and gives its entries to the function you call as discrete arguments.

If you'd done call, thenstring_array would end up with ["a", "b", [0, 1, 2] ]:

const string_array = ['a', 'b'];
const number_array = [0, 1, 2];
string_array.push.call(string_array, number_array);
// −−−−−−−−−−−−−−−^^^^
console.info(string_array); // ["a", "b", [0, 1, 2]]

That's because call is defined differently from apply. Instead of accepting the arguments for the call as an array or array-like object, call accepts them as discrete parameters (like push does). So this:

string_array.push.call(string_array, number_array);

is equivalent to:

string_array.push(number_array);
Ad
source: stackoverflow.com
Ad