Array.prototype 对象的长度 属性 - ES 5
length property of Array.prototype object - ES 5
在function
类型对象中,length
属性 表示function
类型对象期望的参数数量。例如,Function
对象中的 length
字段,Array
对象,在下面的可视化中,具有值 1
.
在上面的可视化中,length
字段也是object
类型对象Array.prototype
的成员,其值为0
.
MDN 说:
Array.prototype.length
reflects the number of elements in an array.
在下面的 cars
& bikes
数组中遵循此定义,
var cars = new Array("Saab", "Volvo", "BMW");
var bikes = ["Honda", "Yamaha"];
cars.__proto__.length
&bikes.__proto__.length
还是0
.
多个数组对象(cars
、bikes
)不能共享相同的 length
属性 值,因为 length
属性 位于Array.prototype
.
1)
根据MDN,这个定义正确吗?
2)
如果不是,Array.prototype
中的 length
字段表示什么?
var cars = new Array("Saab", "Volvo", "BMW");
var bikes = ["Honda", "Yamaha"];
cars.__proto__.length
& bikes.__proto__.length
is still 0.
是的,但是 cars.length === 3
和 bikes.length === 2
。
cars.__proto__.length
是Array
构造函数的prototype
属性的长度。默认情况下这是一个空数组实例。
详情
Array.prototype
是一个空数组实例。
var cars = new Array()
导致 __proto__
指向 Array.prototype
的对象。
所以 cars.__proto__ === Array.prototype
.
在数组实例上,length
是数组对象上最大整数 属性 的值加一。如果为空,则为零。
var a = [];
a[10] = 'foo';
a.length; // 11
Array.prototype
为空。
因此
cars.__proto__.length === 0
MDN says:
Array.prototype.length
reflects the number of elements in an array.
这个定义正确吗?
差不多,是的。与数组相关的三个不同的 .length
属性:
Array.length
。正如你所说,它是一个实例 ("own") 属性,所有 函数 都有,与数组无关。
arr.length
,其中 arr
是数组实例(例如 arr = ['ex', 'ample']
)。每个数组都有这个 special property,它的作用有点像 getter/setter,自动确定对象具有的最高数组索引 属性。
Array.prototype.length
是#2 的特例,因为 Array.prototype
本身只是一个数组对象——默认为空,因此 .length == 0
.
MDN 有点不准确,因为它在文档页面中混合了实例属性和从原型继承的属性。正如您的图表正确显示的那样,所有 cars
、bikes
和 Array.prototype
对象都有自己的 .length
属性 和自己的值(并且更改一个对象不会'当然改变其他人)。
那么Array.prototype.length
有什么用呢?实际上,并不多,因为它通常被继承自 Array.prototype
的数组对象自己的 属性 遮蔽。因此,除了因为规范说 Array.prototype
是一个数组实例而那些具有 属性 之外,它还可以作为 上的合理默认值 .length
从 Array.prototype
继承的普通 (非数组)对象 - 虽然这种情况非常罕见。
在function
类型对象中,length
属性 表示function
类型对象期望的参数数量。例如,Function
对象中的 length
字段,Array
对象,在下面的可视化中,具有值 1
.
在上面的可视化中,length
字段也是object
类型对象Array.prototype
的成员,其值为0
.
MDN 说:
Array.prototype.length
reflects the number of elements in an array.
在下面的 cars
& bikes
数组中遵循此定义,
var cars = new Array("Saab", "Volvo", "BMW");
var bikes = ["Honda", "Yamaha"];
cars.__proto__.length
&bikes.__proto__.length
还是0
.
多个数组对象(cars
、bikes
)不能共享相同的 length
属性 值,因为 length
属性 位于Array.prototype
.
1)
根据MDN,这个定义正确吗?
2)
如果不是,Array.prototype
中的 length
字段表示什么?
var cars = new Array("Saab", "Volvo", "BMW"); var bikes = ["Honda", "Yamaha"];
cars.__proto__.length
&bikes.__proto__.length
is still 0.
是的,但是 cars.length === 3
和 bikes.length === 2
。
cars.__proto__.length
是Array
构造函数的prototype
属性的长度。默认情况下这是一个空数组实例。
详情
Array.prototype
是一个空数组实例。
var cars = new Array()
导致 __proto__
指向 Array.prototype
的对象。
所以 cars.__proto__ === Array.prototype
.
在数组实例上,length
是数组对象上最大整数 属性 的值加一。如果为空,则为零。
var a = [];
a[10] = 'foo';
a.length; // 11
Array.prototype
为空。
因此
cars.__proto__.length === 0
MDN says:
Array.prototype.length
reflects the number of elements in an array.这个定义正确吗?
差不多,是的。与数组相关的三个不同的 .length
属性:
Array.length
。正如你所说,它是一个实例 ("own") 属性,所有 函数 都有,与数组无关。arr.length
,其中arr
是数组实例(例如arr = ['ex', 'ample']
)。每个数组都有这个 special property,它的作用有点像 getter/setter,自动确定对象具有的最高数组索引 属性。Array.prototype.length
是#2 的特例,因为Array.prototype
本身只是一个数组对象——默认为空,因此.length == 0
.
MDN 有点不准确,因为它在文档页面中混合了实例属性和从原型继承的属性。正如您的图表正确显示的那样,所有 cars
、bikes
和 Array.prototype
对象都有自己的 .length
属性 和自己的值(并且更改一个对象不会'当然改变其他人)。
那么Array.prototype.length
有什么用呢?实际上,并不多,因为它通常被继承自 Array.prototype
的数组对象自己的 属性 遮蔽。因此,除了因为规范说 Array.prototype
是一个数组实例而那些具有 属性 之外,它还可以作为 上的合理默认值 .length
从 Array.prototype
继承的普通 (非数组)对象 - 虽然这种情况非常罕见。