Array.length vs Array.prototype.length

I found that both are ArrayObjects and Array.prototypehave a property length. I am confused about using a property Array.length. How do you use it?

Console.log(Object.getOwnpropertyNames(Array));//As per Internet Explorer

outputs:

length,arguments,caller,prototype,isArray,

Prototypeand isArraycan be used, but how do you use the property length?

+1
source share
4 answers

Array is a design function.

All functions have a property lengththat returns the number of declared parameters in the function definition.

+5
source

Array.length - Array(), Array.prototype.length - , . ['foo'].length, Array.prototype.length this, ['foo']

var myArray = ['a','b','c']
console.log(myArray.length); //logs 3
0

Array, Array.prototype JavaScript .

:

function MyClass() {
  this.foo = Math.random();
}

MyClass.prototype.getFoo = function() {
  return this.foo;
}

// Get and log
var bar = new MyClass();
console.log(bar.getFoo());

( ) . . (getFoo), .

, , . length , :

[1, 2, 3, 4].length == 4; // Every array has a length

, , Array . , Array.length, , Array (). length.

0
source

Array.lengthprovides the number of declared parameters in the function definition Array. Since the definition of an Array function has only one parameter size, so it will return 1 regardless of the contents of your array.

Array.prototype.lengthprovides the number of elements in array data. It depends on the contents of the array.

var arr=new Array();
console.log(Array.length);//returns 1
console.log(arr.length);//returns 0 as array has 0 elements
0
source

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


All Articles