BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Scrum
  2. Apache Maven 2
  3. Essential MySQL
  4. Node.js
  5. Groovy
  1. jQuery Selectors
  2. Ajax
  3. Java
  4. Spring Config.
  5. Java Concurrency

Link Details

Link 102117 thumbnail
User 280195 avatar

By nerdyhearn
via nerdyhearn.com
Published: Aug 11 2008 / 15:08

A simple implementation of a Hashtable in javascript.
  • 6
  • 11
  • 7607
  • 6

Comments

Add your comment
User 184684 avatar

kdavies replied ago:

1 votes Vote down Vote up Reply

"Javascript does not have a native type that is comparable to a Hashtable"
Umm, yes it does? All objects in javascript are pretty much hashtables.

User 280195 avatar

nerdyhearn replied ago:

1 votes Vote down Vote up Reply

No, it doesn't. "All objects in javascript are pretty much hashtables" -- So where are the getters and setters? Where are the abilities to list all keys natively? I don't see any functionality that allows a user to access a Javascript table as you would in Java.

User 318695 avatar

richmarr.wordpress.com replied ago:

1 votes Vote down Vote up Reply

If you want to convince people your solution is valuable, the only way to do it is to show them the benefit over the conventional methods, otherwise you just sound like you don't know the conventional methods.


var hash = {};
hash["my key"] = 23487436;
hash["my other key"] = 2633;
for ( var key in hash )
{
alert("key "+ key +": value: "+ hash[key] );
}



User 224257 avatar

wekempf replied ago:

1 votes Vote down Vote up Reply

Not to mention his "solution" isn't what he claims it to be. It's NOT a Hashtable. It's an associative container, yes, but a very poor one (O(n) lookup time?), and NOT a _Hash_ table.

User 318695 avatar

richmarr.wordpress.com replied ago:

0 votes Vote down Vote up Reply

I'd also recommend against casual use of exceptions in browser-based Javascript. The code required can be cumbersome and is often unnecessary. Compare the two following examples.


var myValue = null;
try
{
myValue = hash.get("my key");
}
catch ( Exception e )
{
// potentially do nothing since not finding a value may well be deliberate
}
if ( myValue != null ) doSomething(myValue);


Your code is less concise (hence less readable, and slower to download) than the native non-exception-based solution:


var myValue = hash["my key"];
if ( typeof(myValue) != "undefined" ) doSomething(myValue);

User 282958 avatar

cowardlydragon replied ago:

0 votes Vote down Vote up Reply

Oh Jesus, where to begin.

Dude, you're using Javascript. Loose typing. More compact syntax.

Getters and Setters are an aspect of Java's pseudo-strong typing.

Getter in JS: someobject[somekey]
Setter in JS: someobject[somekey] = somevalue


Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.