Javascript HashTable Encapsulation

Source: Internet
Author: User

 

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.