4

In function type objects, the length property signifies the number of arguments expected by a function type object. For example, the length field in Function object, Array object , in the below visualisation, has the value 1.

enter image description here

In the above visualisation, length field is also a member of object type object Array.prototype, whose value is 0.

MDN says:

Array.prototype.length reflects the number of elements in an array.

Following this definition in below cars & bikes array,

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

cars.__proto__.length & bikes.__proto__.lengthis still 0.

Multiple array objects(cars, bikes) cannot share the same length property value as length property is sitting in Array.prototype.

1)

As per MDN, Is it a right definition?

2)

If no, What does length field in Array.prototype signify?

overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

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

cars.__proto__.length & bikes.__proto__.length is still 0.

Yes, but cars.length === 3 and bikes.length === 2.

cars.__proto__.length is the length of the prototype property of the Array constructor function. Which is an empty array instance by default.

Details

Array.prototype is an empty array instance. var cars = new Array() results in an object with the __proto__ pointing to Array.prototype. So cars.__proto__ === Array.prototype.

On an array instance, length is the value of the largest integer property on the Array object, plus one. Or zero if it is empty.

var a = [];
a[10] = 'foo';
a.length; // 11

Array.prototype is empty.

Hence

cars.__proto__.length === 0
Community
  • 1
  • 1
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1) I did not get you, when you say, *`cars.__proto__.length` is the length of the `prototype` property of the `Array` constructor function.* What does it mean to say `length` of `Array.prototype` property? #### 2) `Array.prototype` is empty? What does this mean? – overexchange Dec 05 '15 at 13:14
2

MDN says:

Array.prototype.length reflects the number of elements in an array.

Is it a right definition?

Sorta, yes. There are three different .length properties related to arrays:

  • Array.length. As you said, it's an instance ("own") property that all functions have, and has nothing to do with arrays specifically.
  • arr.length, where arr is an array instance (e.g. arr = ['ex', 'ample']). Every array has this special property that acts a bit like a getter/setter, automatically determining the highest array index property that the object has.
  • Array.prototype.length is a special case of #2, as Array.prototype is just an array object itself - empty by default, therefore .length == 0.

MDN is a bit inaccurate because it mixes instance properties with those inherited from the prototype in its documentation pages. As your diagram correctly visualises, all of the cars, bikes and Array.prototype objects do have their own .length property with its own value (and changing one doesn't change the others of course).

So what purpose does Array.prototype.length have? Not much, actually, as it's typically shadowed by an own property of the array objects that inherit from Array.prototype. So apart from just being there because the spec says that Array.prototype is an array instance and those have that property, it can also serve as a sensible default .length value on normal (non-array) objects that inherit from Array.prototype - those cases are very rare though.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • `Array.prototype` is just an array instance itself? As this [answer](http://stackoverflow.com/a/11541474) says:*When you put something on the prototype, every instance of the object shares the same code for the method.* What do you mean, when you say, `Array.prototype` is array instance? I am not getting this point. – overexchange Dec 05 '15 at 15:21
  • Yes, it is. Admittedly, one that does not inherit from itself of course, and one that has lots of odd properties (all the Array methods), but it's still an array object in the meaning that it has this special-working `.length` property. – Bergi Dec 05 '15 at 15:23
  • `Array.prototype` is an array object in the meaning that it has `.length` property? It is hard to accept this. Using object literal notation, If I say, `xyz { length : 2 }`, Is `xyz` an array object? `xyz` and `Array.prototype` are both `object` type. – overexchange Dec 05 '15 at 15:32
  • No, the emphasis is on *special* `.length` property. And it also has its internal *[[class]]* set to `"Array"`. See [ES5](http://es5.github.io/#x15.4.4): "*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*". – Bergi Dec 05 '15 at 15:54
  • What is the syntax to find [[Class]] of length property? – overexchange Dec 05 '15 at 17:01
  • @overexchange: see [this answer](http://stackoverflow.com/a/7348639/1048572). The *[[class]]* of `Array.prototype` is `"Array"`, `Array.prototype.length` does not have a *[[class]]* as it is a primitive number. – Bergi Dec 05 '15 at 17:32
  • Answer talks about typeof operator. Applying typeof to Array.prototype gives 'object' type. – overexchange Dec 05 '15 at 17:42
  • @overexchange: The answer I linked to talks about `Object.prototype.toString.call`, not `typeof`. – Bergi Dec 05 '15 at 17:53
  • OK. So, How can i make `.length` of `Array.prototype`more than 0? – overexchange Dec 06 '15 at 13:05
  • 1
    @overexchange: Just `Array.prototype.length = 1` or `.length++` or `Array.prototype[0] = "something"` etc… `Array.prototype` is a fully-functioning array! – Bergi Dec 06 '15 at 13:07
  • So, In addition to having your own array object(like `cars` & `bikes` in my question), what was the idea to provide `Array.prototype` as a fully functioning array? Is there a scenario, where such usage is required? – overexchange Dec 06 '15 at 13:18
  • 1
    See [Why Array.prototype is an Array?](http://stackoverflow.com/q/23652338/1048572), basically the answer is "why not?". You might want to ask the language designers. – Bergi Dec 06 '15 at 13:29