views:

191

answers:

6

I was reading about objects in O'Reilly Javascript Pocket Reference and the book made the following statement.

An object is a compound data type that contains any number of properties.
Javascript objects are associative arrays: they associate arbitrary data values with arbitrary names.

From a language design perspective, if objects are simply associative arrays, then why have objects?

I appreciate the convenience of having objects in the language, but if convenience is the main purpose for adding a data type, then how do you decide what to add and what to not add in a language? A language can quickly become bloated and less valuable if it is weighed down by several overlapping methods and data types (Is this a true statement or am I missing something).

+2  A: 

I think that you're confusing the purpose of that statement. It is meant to describe what an object is, not that javascript does the same thing two different ways. As far as I know, you can't declare an "associative array" in Javascript. If you want one, you create an object.

As far as convenience goes, the question relates to syntax. I would be very unhappy if they got rid of the object "dot" syntax and forced me to use dictionary-style access syntax for everything. But, I don't think that was the point of your question.

More clarification

The way people treat simple javascript arrays is not to be confused with the use of an "associative array", which is effectively a dictionary.

John Fisher
+4  A: 

Objects are more than associative arrays :

  • They can have methods
  • There is some notion of (prototypal) inheritance.

Objects, if you see them only as a mean to store data, are associative arrays ; but they are more than just a way to store data ;-)


Objects are not arrays, actually -- strictly speaking, it's quite the contrary : Array is a specific kind of Object.

Pascal MARTIN
JavaScript objects don't have *methods* per se. They have properties that can refer to functions. It's am important distinction. Also, JavaScript objects *are* "associative arrays." (http://en.wikipedia.org/wiki/Associative_array) That's another term for "map" or "dictionary" (e.g., it doesn't mean the same thing as just "array" which implies numerical indexing).
T.J. Crowder
Plus one for bringing up object prototypes.
justkt
+1  A: 

Can one associative array inherit methods and properties of another array?

Javascript has really powerful objects and functions, i recommend that you watch these:

http://yuiblog.com/crockford/

and read this

http://oreilly.com/catalog/9780596517748

kodisha
A: 
T.J. Crowder
A: 

Sophisticated javascript programmers follow Doug Crockford's (Javascript: the Good Parts) lead and take advantage of Javascript's closures and modularity to build objects that encapsulate their state. If you use Javascript's native objects, you end up with associative arrays that are transparent to their clients. If you have a pointer to the object, you can see all of it's state.

Crockford explains (page 37) how to define objects that hide their state and only reveal info in response to method calls.:

var myObject == function() {
    var value = 0;

   return {
      increment: function(inc) {
          value += typeof inc == 'number' ? inc : 1;
      },
      getValue: function() {
         return value;
      }
   };
}();

myObject is now an opaque object that responds to increment() and getValue() only.

PanCrit
Sophisticated programmers probably don't, except in very limited situations, because of the memory cost involved, which translates (esp. on IE) into slowness. This pattern should never be mentioned without calling out the costs. Every single *instance* will get its *own copy* of those functions. (You can't rely on implementations to reuse the underlying code and just provide a separate execution context.) This translates into real memory costs, even on highly-optimized browsers like Chrome. How much? Try 4.5X-5X as much for just three simple properties (and it rises as you add more).
T.J. Crowder
@T.J. Crowder--What do you do instead? Do you define the function, and then use it as a prototype when creating individual objects?
PanCrit
@PanCrit: I use constructor functions and prototypes, yes. It means that all properties of the instance are public, but also means I get true reuse of the functions. I usually use the module pattern to get private scope for the entire "class" (obviously JavaScript doesn't have classes, but you know what I mean), but not for individual instances, because of the memory cost of the latter. I talk about what I do here http://blog.niftysnippets.org/2009/09/simple-efficient-supercalls-in.html and here http://blog.niftysnippets.org/2009/09/private-methods-in-javascript.html
T.J. Crowder
A: 

Since there are no classes in javascript and everything are objects, including functions, for me it seems somewhat natural that the "base object" can be used as a generic container datastructure called "map" or "dictionary" or "associative array". It's not unique feature of javascript that objects can be used as associative arrays; consider following in PHP:

$map = (object) null;
$map->name = 'value';
unset($map->name);

(Just to make it clear: I'm not comparing PHP to javascript. The former makes absolutely not good example of academic languge.)

jholster