/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] What am I missing in HashTable prototype?

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>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameOct 27.2009 — Change: var myHash = {}; // not correct method to define ???[/QUOTE]
To: [CODE] var myHash = new HashTable();[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorOct 27.2009 — Thank you, 'astupidname',

Worked like a charm.

Must have been one of the only combinations I didn't try. :o

Appreciate the information. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...