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

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.

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.

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.

6 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. " (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 Mar 30 '10 at 16:25 Plus one for bringing up object prototypes. – justkt Mar 30 '10 at 16:33.

Javascript has really powerful objects and functions, I recommend that you watch these: yuiblog.com/crockford/ and read this oreilly.com/catalog/9780596517748.

Also, JavaScript objects are more than just associative arrays (although they are associative arrays — or less confusingly, "maps" or "dictionaries"). They're also hierarchical: If you have A, you can create a B that inherits all of A's properties (properly inherits, as opposed to copying) via prototype inheritance. That's a pretty good reason for having objects as well.

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.

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 Mar 31 '10 at 10:26 @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 Mar 31 '10 at 17:21 1 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 blog.niftysnippets. Org/2009/09/… and here blog.niftysnippets.Org/2009/09/… – T.J.Crowder Apr 2 '10 at 7:01.

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.).

I was reading about objects in O'Reilly Javascript Pocket Reference and the book made the following statement. How do I test for an empty Javascript object from JSON? I need to pass an associative array from Javascript to a C++ activex control in IE6+.

It seems a shame to write my own type for something so standard. Is associative array element the same as an object field? I have an object that looks something like this.

I have this array which is retrieved by a json_encode(). I need to make a Javascript object that would behave as an associative array, but with some functions that are called before getting and setting properties. I want to convert it using Stringify to json object.

I have 5 functions that check user input against regular expressions...these regular expressions have been abstracted out and put here as suggested in previous post. Should I consolidate similar methods into javascript objects(associative arrays)? In C++ I put similar functions in classes.

Should I do this with javascript, except put them in "objects" instead. Otherwise, I just have a collection for related methods. In my script there is a need to create a hash table, I searched in google for this.

Most of the folks are recommending JavaScript object for this purpose. What's the difference between objects and associated array in javascript? Object as associative array...how to get .

I would recommend using an array that is parallel to your object. Each index in the array contains a string key of the object.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions