Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of the `Object.length` property on JavaScript's `Object` constructor? [duplicate]

The documentation for the JavaScript language's built-in global Object object states that

Object.length

Has a value of 1.

What is the significance of this property and, additionally, why isn't object.length available to us in the same manner as array.length?

At first glance, it would seem to me that the reason for my latter question has to do with a possible confusion in the interpreter of the length property on the object with the dot notation method of property access/initialization.

like image 212
IsenrichO Avatar asked Aug 24 '16 05:08

IsenrichO


1 Answers

The length property specifies the number of arguments expected by the function.

As Object belongs to type function and length property of function determines the number of arguments, Object.length returns the length of arguments being expected.


Consider following example:

function test(a, b, c) {}
console.log(test.length);

And with Object:

console.log(typeof Object);
console.log(Object.length); //returns 1

"why isn't object.length available to us in the same manner as array.length?"

Note that array is also of type object(Try typeof []) hence "why isn't object.length available to us" is not literally correct. If you are referring to object-without-indices, we read the object property using the key of the object, if there is no key as length, obj.length returns undefined.

like image 119
Rayon Avatar answered Sep 28 '22 09:09

Rayon