11

A naive confusion:

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

Both objects inherits Array.prototype, but they behave differently with the [] operator. Why?

7
  • 1
    Plain Objects don't calculate length for you like Arrays do... Commented Feb 25, 2014 at 17:47
  • 4
    What a horrible way to create an array! Commented Feb 25, 2014 at 17:47
  • 3
    Javascript Arrays created with Object.create are not real Arrays perfectionkills.com/… & bennadel.com/blog/… Commented Feb 25, 2014 at 17:48
  • 1
    Pity the poor var arr = []. Commented Feb 25, 2014 at 17:48
  • 1
    Of course that var arr =[] is the proper way, but this has nothing to do with the question Commented Feb 25, 2014 at 17:53

2 Answers 2

9

In the first case you create an array object that maintains the length property when you access a integer, non-negative property (index).

In the second case you created a regular object that inherits the Array prototype. Using [] on that object is the same as any object and simply sets regular properties on it.

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.

The ECMAScript 5 Language Spec describes how length is maintained in section 15.4.

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^(32−1).

[...]

Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index;

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Vache. I know about that, but in both cases, the objects created inherit from Array.prototype. As i noted in the example, the method push applied on the object created with Object.create, modifies the inherited property length. Meanwhile the notation with brackets behaves differently.
I probably wasn't clear enough in my answer. The bracket notation isn't a method that is part of the prototype, it's a way to access properties in Javascript. In an Array object (note that inheriting from Array's prototype isn't enough to be an array) the brackets behave differently as it is explained in the spec. Now push and length are in the prototype and that's why calling push on the regular object maintains length.
Fully agree with your comment.
-2

var arr1 = new Array(); is the proper way to instantiate an array. It is the same as using the array literal: var arr1 = [];

1 Comment

OP knows that. He's wondering why the "proper" and the "wrong" way behave differently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.