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"]
All the above mentioned are functions, which has a property length, saying tjhe number of arguments the function takes. Thats why they have length as static variable here.
Array
,Function
, andObject
are all constructors, so they're all functions. Thelength
property of a function specifies the number of (named) arguments that the function takes. From ECMA-262 3rd edition, section 15:And as DCoder pointed out:
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: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 theFunction
constructor, even thoughFunction.length
is equal to1
. The result,identity
, is also a function, whoselength
property is set to the value2
, since it's a function with two arguments.