12

I have seen people using two different ways of using different methods of the Array object in javascript.

I mostly use it like so:

arr.push(element1, ..., elementN)

But I have seen people using this:

Array.prototype.push.apply(this,arguments)

I understand that all JavaScript objects inherit the properties and methods from their prototype. The Object.prototype is on the top of the prototype chain.

What are the differences between two approaches and when should each approach be used?

Skywalker
  • 4,984
  • 16
  • 57
  • 122
  • 1
    The latter is for stuff that isn't an array, like `arguments`. – Dave Newton Oct 27 '15 at 15:04
  • `element1, ..., elementN` is a syntax error. `arguments` is not. – Bergi Oct 27 '15 at 15:04
  • 1
    Pretty sure that was a placeholder ellipses. – Dave Newton Oct 27 '15 at 15:05
  • Modifying the `arguments` object, especially by passing it out of the function to another function, is probably not the best idea in the world in general. – Pointy Oct 27 '15 at 15:07
  • 1
    see [How to push the array of data to another array through javascript without loop](http://stackoverflow.com/q/24979471/1048572) for what it is good for. – Bergi Oct 27 '15 at 15:08
  • 2
    @Pointy: `arguments` is not modifyied here? – Bergi Oct 27 '15 at 15:09
  • @Bergi well adding an entry to it is what I meant; it's kind-of weird. Probably won't hurt anything however. *edit* oh wait I see. Right. Never mind :) – Pointy Oct 27 '15 at 15:18
  • @Bergi The question cited as 'previously existing' has such a different title. Although by some luck it happens to have a related answer. How is the OP supposed to know such a different title would happen to have a answer for this question. To put it another way, just because some other question happens to have the same answer doesn't mean the two questions are the same. – Teddy Mar 21 '18 at 12:13
  • "How to extend Array.prototype.push()? -> I'm trying to extend the Array.push method so that using push will trigger a callback method, then perform the normal array function." Its an unrelated question. – Teddy Mar 21 '18 at 12:19

2 Answers2

13

The call via .apply() is used when you're interested in using .push() with an object that isn't really an array. A jQuery object, for example, is not really an array instance, but the code mostly maintains a .length property that's enough for it to look like an array, at least as far as .push() and other Array prototype methods are concerned.

For a real array instance, there's no need to do that; the .push() method is directly available via the prototype chain.

So:

var obj = { length: 0 };
Array.prototype.push.apply(obj, ["hello world"]);
console.log(obj[0]); // hello world
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Thank you so much for the answer. Could you provide a little more explanation or an example of array.prototype.push being used in a scenario in pure javascript. I am currently staying away from jQuery I want to go through pure javascript first. I do understand what you have said but a very simple example/scenario would be extremely helpful. Again thank you so much. – Skywalker Oct 27 '15 at 15:08
  • 1
    @LorenzovonMatterhorn You *provided* an example of it being used in pure JS. – Dave Newton Oct 27 '15 at 15:12
  • @LorenzovonMatterhorn jQuery is just JavaScript, and it's just an example. Any object that has a `.length` property that's an integer value will do. – Pointy Oct 27 '15 at 15:17
  • @DaveNewton I was hoping for a very basic, nothing too fancy jsFiddle example. Something I can mess around with. And I can see it in action as how it worked. – Skywalker Oct 27 '15 at 15:18
  • @LorenzovonMatterhorn Copy what you have into a function in jsfiddle then call that function with arguments. – Dave Newton Oct 27 '15 at 15:29
2

I assume you saw Array.prototype.push.apply(this,arguments) in an function like this

function foo() {
    arguments.push = function() {
       Array.prototype.push.apply(this,arguments);    
    } 
    arguments.push(1,2,3);
    //.... 
}

Here this is the foo's arguments, it is just an Array like object, not an Array, it does not have push method. So we should use Array.prototype.push.apply(this,arguments)

bpceee
  • 416
  • 4
  • 17
  • Thank you for the reply. could you explain what you mean by an "array like object"? – Skywalker Oct 27 '15 at 15:10
  • It doesn't matter that `arguments` doesn't have a push method? How do you think could one rewrite this code if it had? – Bergi Oct 27 '15 at 15:10
  • @LorenzovonMatterhorn Array-like objects look like arrays. They have various numbered elements and a length property. But that’s where the similarity stops. Array-like objects do not have any of Array’s functions, and for-in loops don’t even work! Check out these article: http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/ – bpceee Oct 27 '15 at 15:11
  • 2
    @bcpeee But this is not what's happening. `arguments` is not the object that the `push` method is applied to. `this` is. – basilikum Oct 27 '15 at 15:18
  • @basilikum You are right, so maybe `this` is not an Array in this case. Sorry I made a bad example, checkout the duplicated version question. – bpceee Oct 27 '15 at 15:24