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.