views:

698

answers:

11

In Javascript, what is the difference between an object and a hash? How do you create one vs the other, and why would you care? Is there a difference between the following code examples?

var kid = {
 name: "juni",
 age: 1
}

And:

var kid = new Object();
kid.name = "juni";
kid.age = 1;

And:

var kid = new Object();
kid["name"] = "juni";
kid["age"] = 1;

Can you think of any other code example I should illustrate?

The core question here is what is the difference between an object and a hash?

+4  A: 

They are the same.

you can use them interchangeably.

Jimmy Chandra
+13  A: 

There just isn't any. All three of those are literally equal.

Marcin
+4  A: 

There isn't any difference in any of your samples. They are all objects with named properties. You've just shown different ways of creating/referencing those properties.

tvanfosson
+3  A: 

I think this is all the same. The third version could used with dynamic property names. The first one is the shortest to write.

Hippo
+6  A: 

They are different notation systems that you can use interchangeably. There are many situations where using the bracket syntax [ ] can be more appealing, an example would be when referencing an object with a variable.

var temp  = "kid";
var obj = new Object();
obj[temp] = 5; // this is legal, and is equivalent to object.kid
obj.temp = 5; // this references literally, object.temp
Ian Elliott
+1  A: 

They are the same. Just as [] and new Array() are the same.

For more information on the core types of JavaScript, have a look at the MDC Core JavaScript 1.5 reference.

If you want proof that {} is the same as new Object():

Object.prototype.helloWorld = function () { alert('Foo!'); };
var a = new Object();
var b = {};
a.helloWorld();
b.helloWorld();

!!! WARNING ACHTUNG AVERTISSEMENT !!! Never, ever assign to the prototype property of the Object type in production code. You'll be polluting the whole global namespace.

Blixt
+1  A: 

Have a look here. I think it's pretty darn clear. From the first few lines on that site:

"Hash can be thought of as an associative array, binding unique keys to values ... Because of the nature of JavaScript programming language, every object is in fact a hash; but Hash adds a number of methods that let you enumerate keys and values, iterate over key/value pairs, merge two hashes together, encode the hash into a query string representation, etc."

Matt Ball
A: 

Actually, there is nothing called 'hashtable' or 'hashmap' in JavaScript. The object in JavaScript behaves like a 'hash' [objects in JavaScript are simply key/value properties] and hence the confusion.

SolutionYogi
+1  A: 

Actually, every object in JavaScript IS a hash. This is a hash of object's properties and methods. In fact, everything in Javascript is a hash (i.e a list of name/value pairs).

Every time you call object's method, property, or just reference any variable, you perform internal hash lookup.

Thevs
+2  A: 

In other languages such as Java and C# it's possible to use any object (not just a string or a number) as a key in a hash table/hash map, which is not the case in JavaScript: keys are simply converted to strings.

var h = {}, k = {};
h[k] = "One";
alert( h[ "[object Object]" ] ); // Alerts "One"

It can be useful to use arbitrary objects as keys, in which case you can use something like jshashtable.

Disclaimer: I wrote jshashtable.

Tim Down
A: 

Technically, they are the same. When you write code, you can easily do myobject['someproprty' + 'somethingElseConcatenated], which you cannot do when using the "dot notation" - myobject.someproperty is all you can do.

Douglas Crockford, one of autors of ECMAscript, suggests not to use var a = new Object() syntax for some reason I didn't quite catch. Anyway, it's worth watching his presentation if you're interested in it (it consists of several parts, the first one is here http://video.yahoo.com/watch/111593/1710507)

naivists