Code
<Html>
<Head>
<Title> Example-8.3 simple HashTable class </title>
</Head>
<Body>
<Script>
<! --
Function dwn (s)
{
Document. write (s + "<br/> ");
}
// Simple HashTable type
Function HashTable (){
// Special keywords (specialKey) are used to process special reserved words.
// These reserved words are the inherent attributes and methods of the Object.
Var specialkey = [
"ValueOf ",
"HasOwnProperty ",
"IsPrototypeOf ",
"PropertyIsEnumerable ",
"Prototype ",
"Constructor ",
"ToLocaleString ",
"ToString"
];
// Provide separate storage space for special keywords
Var specialvalue = new Array (specialkey. length );
// Special keyword accesskey. true indicates that a value already exists and cannot be inserted again.
Var specialflag = new Array (specialkey. length );
// Store common keywords. Only one common Object is required.
Var normalHashtable = {};
// Insert values into the Hash table
This. insert = function (key, value)
{
// Process special keywords
For (var I = 0; I <specialkey. length; I ++)
{
If (key = specialkey [I]) // if the keyword is equal to a special keyword
{
If (! Specialflag [I]) // if there is no value for this position
{
Specialvalue [I] = value; // insert a value
Specialflag [I] = true; // set a special flag
Return true;
}
Else return false;
}
}
// Process common keywords
If (key in normalHashtable) return false;
NormalHashtable [key] = value;
Return true;
}
// Search in the Hash table
This. find = function (key)
{
// Process special keywords first
For (var I = 0; I <specialkey. length; I ++)
{
If (key = specialkey [I])
{
Return specialvalue [I];
}
}
// Search for Common keywords
Return normalHashtable [key];
}
}
Var t1 = new HashTable ();
Dwn (t1.insert ("prototype", "idea "));
Dwn (t1.insert ("prototype", "idea "));
Dwn (t1.find ("prototype "));
-->
</Script>
</Body>
</Html>