Noob question about Array.prototype

When you create an object from another object in JavaScript the new object has access to what is in the prototype of the object that was copied without having to make its own copy.

the methods (functions) belonging to an object are normally attached to the prototype so that no matter how many copies you make of the object that all of them can access the same copy of those methods rather than each having to have their own copy.
Properties are not normally placed in the prototype as you usually need each copy of the object to have its own set of values rather than sharing them between objects.

So in the case of Array.prototype.pop() it means that the one copy of the code to do the pop() is shared between all arrays whereas the property Array.length is not on the prototype because you want different arrays to be able to have different lengths.

The reason for representing it as Array.prototype.pop() is because pop() is attached to the prototype of Array and not to Array directly the way length is attached directly.

As an example, if I include the following code in a web page:

Array.prototype.shuffle = function() {
   var r=[],c = this.slice(0);
   while (c.length) r.push(c.splice(Math.random() * c.length, 1)[0]);
   return r;
}; 

this adds a new method shuffle() to the Array prototype which is therefore available for ALL arrays in the code in the page to use (even if they were created before this code runs). If we omit the reference to prototype then we are only adding the method to the Array object and it is not shared with any existing arrays.

2 Likes