[thelist] Extending Javascript Objects

marcus m-lists at bristav.se
Sat Aug 20 14:01:21 CDT 2005


Since javascript is javascript (ie Lisp light) you miss one (or several) 
important method(s):

function SimpleHashtable_each(callback) {
   for(var i = 0 ; i < this._keys.length ; i++) {
     var key = this._keys[i];
     callback(key, this.get(key));
   }
}
SimpleHashtable.prototype.each = SimpleHashtable_each;

etc...

Usage:

var hash = new SimpleHashtable();
hash.put("a", "value a");
hash.put("b", "value b");
hash.each(function(key, value) {
   alert(key + " = " + value);
}); // => alerts a = value a  and b = value b

Completely untested but should work.

A question about the implentation:

Since you are indeed using an Object as the a hash internally, aren't 
you sort of speaking against yourself in this case?

And I think it's true that you shouldn't use Object as a hash (at least 
from a design point of view, perhaps not from a performance point but I 
guess that dependes on the implementation of the js interpretator)

/Marcus



More information about the thelist mailing list