P K*_*P K 6 javascript oop semantics
Function,Array和Object构造函数的长度静态属性是什么?
静态方法有意义,但长度静态属性呢?
Object.getOwnPropertyNames(Array)
["length", "name", "arguments", "caller", "prototype", "isArray"]
Object.getOwnPropertyNames(Function)
["length", "name", "arguments", "caller", "prototype"]
Run Code Online (Sandbox Code Playgroud)
注意:我得到的是关于Function.prototype的长度属性的答案,这里没有提到.
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"]
Run Code Online (Sandbox Code Playgroud)
Array,Function并且Object都是构造函数,因此它们都是函数.length函数的属性指定函数采用的(命名)参数的数量.从ECMA-262第3版第15节开始:
本节中描述的每个内置Function对象 - 无论是构造函数,普通函数还是两者 - 都有一个length属性,其值为整数.除非另行指定,否则此值等于函数描述的标题部分中显示的最大命名参数数,包括可选参数.
正如DCoder指出的那样:
ECMA-262第3版,第15.2.3节,第15.3.3节和第15.4.3节规定所有这些构造函数都有一个length属性,其值为1.
关于静态字段的观点:JavaScript中没有静态字段,因为JavaScript中没有类.只有原始值,对象和函数.对象和函数(也表现为对象)具有属性.
可能令人困惑的一件事Function实际上是一个功能.一个鲜为人知的事实是您可以使用此构造函数创建函数.例如:
var identity = new Function("a", "b", "return a")
console.log(identity(42))
Run Code Online (Sandbox Code Playgroud)
以上将打印42.现在注意两件事:Function实际上接受论证并用它们做点什么; 并且我向Function构造函数传递了多个参数,即使Function.length它等于1.结果,identity也是一个函数,其length属性设置为值2,因为它是一个带有两个参数的函数.