I found the following script in the referenced blog.
I added the testHashTable() function script in an effort to use it.
Looked easy enough to implement, but I’m now confused.
I’m getting an error in the creation of the hash (myHash in particular)
in the lines just after the “function testHashTable”, but I don’t know what to change.
Can someone tell me the error of my ways please. ?
[code]
<html>
<head>
<title>HashTable Sample</title>
<script type=”text/javascript”>
// From: http://erik.eae.net/archives/2005/06/06/22.13.54/
function HashTable() { this._hash = {}; }
HashTable.prototype.add = function (key, val) { this._hash[key] = val; };
HashTable.prototype.remove = function (key) { delete this._hash[key]; };
HashTable.prototype.getItem = function (key) {
if (this._hash.hasOwnProperty(key)) {
return this._hash[key];
}
return undefined;
};
HashTable.prototype.containsKey = function (key) { return this._hash.hasOwnProperty(key); };
HashTable.prototype.getKeys = function () {
var res = [];
for (var k in this._hash) {
if (this._hash.hasOwnProperty(k)) { res.push(k); }
}
return res;
};
HashTable.prototype.getValues = function () {
var res = [];
for (var k in this._hash) {
if (this._hash.hasOwnProperty(k)) { res.push(this._hash[k]); }
}
return res;
};
</script>
<script type=”text/javascript”>
function testHashTable() {
var myHash = {}; // not correct method to define ???
// myHash.HashTable(); // this does not work
// HashTable(myHash); // this causes en error with .add below
myHash.add(‘Alley, Gasoline’,’08/16/1947′);
myHash.add(‘Boop, Betty’,’07/30/1951′);
myHash.add(‘Cow, Cash’,’05/29/1970′);
myHash.add(‘Duck, Daffy’,’08/03/1971′);
alert(‘Keys:nn’+myHash.getKeys());
alert(‘Values:nn’+myHash.getValues());
}
</script>
</head>
<body>
<button onclick=”testHashTable()”>Test HashTable Creation</button>
</body>
</html>