new Array()vs Object.create(Array.prototype)

fer*_*ito 8 javascript

天真的困惑:

var arr1 = new Array();
var arr2 = Object.create(Array.prototype);
//Inserting elements in "both arrays"
arr1[0] =0;
arr1[9] =9;
arr2[0] =0;
arr2[9] =9;
arr1.push(10);
arr2.push(10);
console.log(arr1.length); // prints 11
console.log(arr2.length); // prints 1
Run Code Online (Sandbox Code Playgroud)

两个对象都继承了Array.prototype,但它们与[]运算符的行为不同.为什么?

dee*_*see 8

在第一种情况下,您创建一个数组对象,length在访问整数非负属性(索引)时维护该属性.

在第二种情况下,您创建了一个继承原型的常规对象Array.使用[]该对象是一样的任何对象,简单地设置就可以了常规属性.

var arr1 = new Array(); // or var arr1 = [];
arr1[0] = 0;
arr1['foo'] = 3;
// arr1 has a length of 1 because 0 is an array index and 'foo' is a regular property.

var arr2 = Object.create(Array.prototype);
arr2[0] = 0;
arr2['foo'] = 3;
// arr2 has a length of 0 because both 0 and 'foo' are regular properties.
Run Code Online (Sandbox Code Playgroud)

ECMAScript 5语言规范描述了如何length第15.4节中维护.

数组对象对特定类的属性名称进行特殊处理.当且仅当ToString(ToUint32(P))等于P且ToUint32(P)不等于2 ^(32-1)时,属性名P(以String值的形式)是数组索引.

[...]

具体来说,每当添加名称为数组索引的属性时,如果需要,将更改length属性,使其大于该数组索引的数值;