I'm doing some web development, and I'd like to use an associative array in my code. I've used hashtables in other design work, and they definitely do t开发者_运维百科he trick.
However, when I try to call "var coms = new Hashtable();", I get errors stating that there is no class hashtables.
I've read that in JS all objects ARE hashtables, so I suppose if I were to define an empty object, and name it Hashtable, I would be good to go. I'm going to try that n ow. However, it would be nice if someone could tell me how to call an official hashtable.
You can say var coms = {}
or var coms = new Object()
Objects are hashtables in JS
coms.something = 1
is the same thing as coms["something"] = 1
The bracket notation is usually more commonly used for "hashtables" in JS since it's used for associations like var coms = {"something": 1}
and is used in languages like Python for the actual hashtable/dict representation.
There is a JavaScript hash table implementation called jshashtable, written by me. Unlike a JavaScript Object
, it allows any object to be used as a key.
var h = new Hashtable();
var o = {};
h.put(o, "Some value");
h.put("foo", 23);
alert(h.get(o)); // "Some value"
Or var coms
, for that matter
精彩评论