[ACCEPTED]-Difference between an object and a hash?-json
There just isn't any. All three of those 1 are literally equal.
They are different notation systems that 4 you can use interchangeably. There are many 3 situations where using the bracket syntax 2 [ ]
can be more appealing, an example would 1 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 7 possible to use any object (not just a string 6 or a number) as a key in a hash table/hash 5 map, which is not the case in JavaScript: keys 4 are simply converted to strings.
var h = {}, k = {};
h[k] = "One";
alert( h[ "[object Object]" ] ); // Alerts "One"
It can be 3 useful to use arbitrary objects as keys, in 2 which case you can use something like jshashtable.
Disclaimer: I 1 wrote jshashtable.
Actually, every object in JavaScript IS 6 a hash. This is a hash of object's properties 5 and methods. In fact, everything in Javascript 4 is a hash (i.e a list of name/value pairs).
Every 3 time you call object's method, property, or 2 just reference any variable, you perform 1 internal hash lookup.
There isn't any difference in any of your 3 samples. They are all objects with named 2 properties. You've just shown different 1 ways of creating/referencing those properties.
They are the same.
you can use them interchangeably.
0
I think this is all the same. The third 3 version could used with dynamic property 2 names. The first one is the shortest to 1 write.
They are the same. Just as []
and new Array()
are the 6 same.
For more information on the core types 5 of JavaScript, have a look at the MDC Core JavaScript 1.5 reference.
If you 4 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 3 assign to the prototype
property of the Object
type in 2 production code. You'll be polluting the 1 whole global namespace.
Technically, they are the same. When you 8 write code, you can easily do myobject['someproprty' + 'somethingElseConcatenated]
, which you 7 cannot do when using the "dot notation" - myobject.someproperty
is 6 all you can do.
Douglas Crockford, one of 5 autors of ECMAscript, suggests not to use 4 var a = new Object()
syntax for some reason I didn't quite catch. Anyway, it's 3 worth watching his presentation if you're 2 interested in it (it consists of several 1 parts, the first one is here http://video.yahoo.com/watch/111593/1710507)
Every engine(browser) implements it differently 7 but let focus on chrome's V8(based on performance 6 tests I performed a year ago on most of 5 the modern browsers they give similar performance 4 boosts if you follow v8 guidlines).
What 3 it basically happens is:
- To implement a dynamic object on which properties can be added and deleted on the fly - a hashtable is the simplest solution - but speedwise its not as efficient as a regular object in java(random access...).
- What V8 does is trying to guess based on few strategies if your object is a regular object(has final set of properties set in specific order, etc...) or a hashtable. At first it assumes this is a simple object and each new property causes creation of a new structure of an object and copying of the old one on it plus the new property. If the object is categorize as "difficult" - it is moved to a hushtable.
- If v8 notices too many "mistakes" - it moves everything to hashtables - therefore you get poor performance (thats why you want to use constructors that init all of your members or structures inited in a json...)
Please see this 2 links: https://developers.google.com/v8/design#prop_access
www.quora.com/Are-JavaScript-Objects-implemented-as-HashTables-Is-key-value-access-O-1
jayconrod.com/posts/52/a-tour-of-v8-object-representation
Also 1 a very good lecture: https://www.youtube.com/watch?v=UJPdhx5zTaw
Hope it helps...
Actually, there is nothing called 'hashtable' or 4 'hashmap' in JavaScript. The object in JavaScript 3 behaves like a 'hash' [objects in JavaScript 2 are simply key/value properties] and hence 1 the confusion.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.