Difference between an object and a hash?

Viewed 22815

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?

11 Answers

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

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

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.

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.

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.

They are the same.

you can use them interchangeably.

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

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.

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)

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.

Related