Length of Array.prototype - ES 5

In type objects, a functionproperty lengthmeans the number of arguments expected by a type object function. For example, the field of an lengthobject function, the object Array, in the visualization below, matters 1.

enter image description here

In the above visualization, the field is lengthalso a member of the objecttype object Array.prototypewhose value 0.

MDN says:

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

Following this definition below carsand bikesarray,

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

cars.__proto__.lengthand bikes.__proto__.lengthstill 0.

Multiple array objects ( cars, bikes) cannot use the same property value lengthbecause the property lengthis in Array.prototype.

1)

According to MDN , is this the correct definition?

2)

If not, what does the field mean lengthin Array.prototype?

+4
source share
2 answers

MDN says:

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

Is this the correct definition?

Varieties, yes. There are three different properties .lengthassociated with arrays:

  • Array.length. As you said, this is an instance property ("native") that has all the functions and has nothing to do with arrays.
  • arr.length, arr - (, arr = ['ex', 'ample']). , /, , .
  • Array.prototype.length # 2, Array.prototype - , .length == 0.

MDN , . , cars, bikes Array.prototype .length ( , , ).

. Array.prototype.length? , , , Array.prototype. , , , Array.prototype , , .length ( ) , Array.prototype - , .

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

cars.__proto__.length bikes.__proto__.length - 0.

, cars.length === 3 bikes.length === 2.

cars.__proto__.length - prototype Array. .

Array.prototype - . var cars = new Array() __proto__, Array.prototype. cars.__proto__ === Array.prototype.

length - Array, . , .

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

Array.prototype .

cars.__proto__.length === 0
+3

Source: https://habr.com/ru/post/1618697/


All Articles