Function,Array和Object构造函数的length属性是什么? [英] What is the length property of the Function, Array, and Object constructors?

查看:118
本文介绍了Function,Array和Object构造函数的length属性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Function,Array和Object构造函数的长度静态属性是什么?

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"]

注意:我在这里没有得到关于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"]


推荐答案

数组函数对象都是构造函数,所以他们都是功能。函数的 length 属性指定该函数采用的(命名)参数的数量。从ECMA-262第3版第15节开始:

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:


本节中描述的每个内置Function对象 - 无论是构造函数,还是
普通函数或两者都有一个长度属性,其值为整数。除非另有说明,否则
此值等于函数描述的标题部分中显示的最大命名参数数,包括可选参数。

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.

正如DCoder指出:

And as DCoder pointed out:


ECMA-262第3版,第15.2.3节,第15.3.3节和第15.4节.3指定所有这些构造函数都有一个length属性,其值为1.

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.

关于静态字段的观点:没有像JavaScript中的静态字段,因为JavaScript中没有类。只有原始值,对象和函数。对象和函数(表现为对象)具有属性

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.

可能令人困惑的一件事是 Function 实际上是一个函数。一个鲜为人知的事实是您可以使用此构造函数创建函数。例如:

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))

以上将打印 42 。现在注意两件事:函数实际上接受参数并对它们做一些事情;并且我向 Function 构造函数传递了多个参数,即使 Function.length 等于 1 。结果 identity 也是一个函数,其 length 属性设置为值 2 ,因为它是一个带有两个参数的函数。

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.

这篇关于Function,Array和Object构造函数的length属性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆