Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the length property of the Function, Array, and Object constructors?

What is length static property of Function,Array and Object constructor?

Static methods makes sense but what about length static property?

Object.getOwnPropertyNames(Array)
["length", "name", "arguments", "caller", "prototype", "isArray"]

Object.getOwnPropertyNames(Function)
["length", "name", "arguments", "caller", "prototype"]

Note: I am getting answers about length property of Function.prototype that is not asked here.

Object.getOwnPropertyNames(Function.prototype)
["length", "name", "arguments", "caller", "constructor", "bind", "toString", "call", "apply"]

Object.getOwnPropertyNames(Object)
["length", "name", "arguments", "caller", "prototype", "keys", "create", "defineProperty", "defineProperties", "freeze", "getPrototypeOf", "getOwnPropertyDescriptor", "getOwnPropertyNames", "is", "isExtensible", "isFrozen", "isSealed", "preventExtensions", "seal"]
like image 301
P K Avatar asked Dec 27 '12 06:12

P K


People also ask

Does array have length property?

length is a property of arrays in JavaScript that returns or sets the number of elements in a given array. The length property of an array can be returned like so. The assignment operator, in conjunction with the length property, can be used to set the number of elements in an array like so.

How do you find the length of an array of objects?

You can simply use the Object. keys() method along with the length property to get the length of a JavaScript object. The Object. keys() method returns an array of a given object's own enumerable property names, and the length property returns the number of elements in that array.

What is length () function?

The LENGTH() function returns the length of a string (in bytes).

Is array length a method or property?

length is a property of array, not a method if it was a method getting the length of an array would be of O(n), but by keeping it as a property its O(1).


1 Answers

Array, Function, and Object are all constructors, so they're all functions. The length property of a function specifies the number of (named) arguments that the function takes. From ECMA-262 3rd edition, section 15:

Every built-in Function object described in this section—whether as a constructor, an ordinary function, or both—has a length property whose value is an integer. Unless otherwise specified, this value is equal to the largest number of named arguments shown in the section headings for the function description, including optional parameters.

And as DCoder pointed out:

ECMA-262 3rd edition, sections 15.2.3, 15.3.3 and 15.4.3 specify that all these constructors have a length property, whose value is 1.

To your point about static fields: There is no such thing as static fields in JavaScript, as there are no classes in JavaScript. There are only primitive values, objects, and functions. Objects and functions (which behave as objects as well) have properties.

One thing that may be confusing is that Function is in fact a function. A little-known fact is that you can create functions using this constructor. For example:

var identity = new Function("a", "b", "return a")
console.log(identity(42))

The above will print 42. Now notice two things: Function actually takes arguments and does something with them; and I passed more than one argument to the Function constructor, even though Function.length is equal to 1. The result, identity, is also a function, whose length property is set to the value 2, since it's a function with two arguments.

like image 59
seliopou Avatar answered Oct 18 '22 04:10

seliopou