Question

In the below visualisation,

enter image description here

There are two array objects(cars & bikes) that are created with below syntax,

var cars = new Array("Saab", "Volvo", "BMW");
var bikes = ["Honda", "Yamaha"];

whose [[Class]] property value is Array.

In addition, we also have Array.prototype, which is fully functional array, as shown below,

> Object.prototype.toString.call(Array.prototype);
      "[object Array]"
> Array.prototype[0] = "Volvo";
      "Volvo"
> Array.prototype[1] = "BMW";
      "BMW"
> Array.prototype.length;
      2

Generally, When you put something on the prototype, every instance of the object shares the same properties.

Question:

With length property as member, What is the idea behind Array.prototype being fully functional array?

Was it helpful?

Solution

The Array-prototype is itself a fully functional array, because it need to contain all the functionality which is necessary for an object to function as an array. The Array-instances inherit all their functionality through the prototype.

According to the spec:

The Array prototype object is itself an array; its [[Class]] is "Array", and it has a length property (whose initial value is +0) and the special [[DefineOwnProperty]] internal method described in 15.4.5.1.

Licensed under: CC-BY-SA with attribution
scroll top