Alex Rivera | Logout

Why can I add named properties to an array as if it were an object?

Asked 2009-05-17T09:11:29.503
112

The following two different code snippets seem equivalent to me:

var myArray = Array();
myArray['A'] = "Athens";
myArray['B'] = "Berlin";

and

var myObject = {'A': 'Athens', 'B':'Berlin'};

because they both behave the same, and also typeof(myArray) == typeof(myObjects) (both yield 'object').

Is there any difference between these variants?

Edit
Report

3 Answers

137

Virtually everything in javascript is an object, so you can "abuse" an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays are for numerically indexed data - for non-numeric keys, use an Object.

Here's a more concrete example why non-numeric keys don't "fit" an Array:

var myArray = Array();
myArray['A'] = "Athens";
myArray['B'] = "Berlin";

alert(myArray.length);

This won't display '2', but '0' - effectively, no elements have been added to the array, just some new properties added to the array object.

answered 2009-05-17T09:29:11.647
5

Me thinks, me too metaphorical and cryptic with previous answer. Clarification follows.

An instance of Array, Boolean, Date, Function, Number, RegExp, String is an Object but enhanced with methods and properties specific to each type. For example, an array has a predefined length property while generic objects do not.

javascript:alert([].length+'\n'+{}.length)

displays

0
undefined

Intrinsically, the FF Gecko interpreter also distinguishes between Arrays and generic Objects with distinct differences evaluating language constructs.

javascript:
  ra=[  "one",   "two",   "three"]; ra.a=4;
  ob={0:"one", 1:"two", 2:"three"}; ob.a=4;
  alert(
    ra            +"\n\n"+
    ob            +"\n\n"+
    ra.toSource() +"\n\n"+
    ra.a          +"\t .toSource() forgot me! \n\n"+
    ra.length     +"\t and my length! \n\n"+
    ob.toSource());
  ps=""; for(i in ra)ps+=i+" "; alert(ps);  /* NB .length is missing! */
  ps=""; for(i in ob)ps+=i+" "; alert(ps);

displaying

one,two,three

[object Object]

["one", "two", "three"]

4    .toSource() forgot me! 

3    and my length! 

({0:"one", 1:"two", 2:"three", a:4})

and 0 1 2 a and 0 1 2 a.

Regarding the statement that all objects are functions:

It is neither syntactically nor semantically correct to use an arbitrary object instance as a function like 123() or "abc"() or []() or {}() or obj() where obj is any type other than Function, so an arbitrary object INSTANCE is not a Function. However, given an object obj and it's type as Array, Boolean, Date, ..., how did obj come to be as an Array, Boolean, Date, ...? What is an Array, Boolean, Date, ...?

javascript:
    alert([Array
answered 2011-05-17T20:06:13.587
-1

The {}-notation is just syntactical sugar to make the code nicer ;-)

JavaScript has many similar constructs like the construction of functions, where function() is just a synonym for

var Func = new Function("<params>", "<code>");
answered 2009-05-17T09:14:33.833

Your Answer