A complete example of the Map set tool class implemented by Javascript, and a map tool class

Source: Internet
Author: User

A complete example of the Map set tool class implemented by Javascript, and a map tool class

This example describes the Map set tool class implemented by Javascript. Share it with you for your reference. The details are as follows:

Var Map = function () {// construct the entry object var Entry = function (key, value) {this. key = key; this. value = value;} this. entries = new Array (); // construct the put method and put an Entry this in the Array. put = function (key, value) {// If an array already exists, the for (var I = 0; I <this. size (); I ++) {if (this. entries [I]. key === key) {return false ;}} this. entries. push (new Entry (key, value) ;}; // simulate the get method this. get = function (key) {for (var I = 0; I <This. size (); I ++) {if (this. entries [I]. key = key) {return this. entries [I]. value ;}} return null ;}; // you can call this operation to find the tag value. indexOf = function (key) {var index =-1; for (var I = 0; I <this. size (); I ++) {if (this. entries [I]. key = key) {index = I; break;} return index;} // delete an element this. remove = function (key) {var index = this. indexOf (key); if (index! =-1) {this. entries. splice (index, 1) ;}// obtain the map length this. size = function () {return this. entries. length ;}; // reset the key-value pair this. setValue = function (key, value) {var index = this. indexOf (key); if (index! =-1) {this. entries [I]. value = value ;};}; // whether it is empty map this. isEmpty = function () {return this. size () <= 0 ;}; // clear map; this. clear = function () {this. entries = [] ;}; // obtain the entry object this. getEntry = function (index) {if (index> = 0 & index <this. size () {return this. entries [index];} return null;} this. toString = function () {var str = "["; for (var I = 0; I <this. size (); I ++) {str + = this. getEntry (I ). key + "=" + this. getEntry (I ). value + ",";} // remove the last "," str = str. substring (0, str. length-1); str + = "]"; return str ;};}

I hope this article will help you design javascript programs.

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.