简体   繁体   中英

length property of Array.prototype object - ES 5

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 .

在此输入图像描述

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__.length is 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?

 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

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM