5

As I think, JS array is just a hash-map which accepts only integral value as a key. And the .length property just return largest index + 1.

Is this right? Is there any other differences?

eonil
  • 83,476
  • 81
  • 317
  • 516

4 Answers4

3

You are wrong; arrays can have any keys you want.

Also, they inherit the Array prototype.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Well I would not say that arrays can have any keys. I think integer keys are treated in a special way, whereas if you use another key, like a string, you just make use of the fact that an array is also just an object. This is reflected with `Array.length` as the OP already said. – Felix Kling Jul 30 '10 at 12:29
  • @Felix: The only visible special treatment of integer keys is the `length` property. Although implementations may optimize usage of integer keys, such optimizations must have no side-effects. – SLaks Jul 30 '10 at 12:40
  • So, it seems to be safe to treat Array as a special case of hash-map. – eonil Jul 30 '10 at 12:50
1

A JavaScript Array also inherits from Object, so it will get all the capabilities of an object. JavaScript Arrays have additional functionality though:

var myA = ['foo', 'bar', 'baz'];
var myO = {0: 'foo', 1: 'bar', 2: 'baz'};

// these both give us "foo":
console.log(myA[0]);
console.log(myO[0]);

// array has additional methods, though:
console.log(myA.pop());
console.log(myO.pop()); // <- error

While you can add integer properties to regular Objects and add non-integer properties to Arrays, this won't give an Object the special properties and methods that Array has, and Array's special functionality only applies to its integer-keyed properties.

A good reference to all the extra properties that Arrays inherit is the Mozilla Developer Center article on Array. Make sure you pay attention to the little "non-standard" and "Requires JavaScript 1.x" notes if you want to maintain cross-browser compatibility.

Neall
  • 26,428
  • 5
  • 49
  • 48
  • myO.pop = function() { return "ne ne ne ne, I can pop too!" }; - here you go, an object (not array) with perfectly working .pop(). Your code doesn't prove anything. – Piotr Rochala Jul 30 '10 at 13:02
1

The difference is:

Object.prototype.toString.call([]); // [object Array]
Object.prototype.toString.call({}); // [object Object]

Edit:

Also, have a look at this section from ECMAScript specifications as it precisely explains what an Array is: http://bclary.com/2004/11/07/#a-15.4

Piotr Rochala
  • 7,748
  • 2
  • 33
  • 54
  • Just the information what I've been found. Now I can treat Array as just a has-table, not a C style array. Thanks. – eonil Jul 30 '10 at 13:09
0

Array objects can have any property that an object can have. The only special property is the "length" property that is (potentially) updated when you set an "array index" property, and that can also be used to remove array elements if set to a lower value than its current.

"Array indices" are strings (all object properties are) that is the canonical decimal representation of an unsigned integer in the range 0..2^32-2 (i.e., "0" to "4294967294"). The limit is one below the maximal value of a 32-bit unsigned value because the length field value is then always an unsigned 32-bit integer value.

Array objects also inherit from Array.prototype (but you can make other objects that do that too, if you want) and their internal class is "Array".

I.e, in practice, the only difference between an Array and a plain Object instance is the "magical length property". If you don't need that for anything, you should just use an object.

  • myObject.length = function() { return 12 }; - cool, I'm an array now. Again - your post does not prove anything and what you say is simply wrong. – Piotr Rochala Jul 30 '10 at 13:14