Map toolkit for javascript implementation
Last Update:2014-06-10
Source: Internet
Author: User
[Javascript] (function () {window [& amp; #39; XR & amp; #39;] {}; varmidContainernewArray (); varmapContainernewArray (); varMAPID0; functionMap (mid ){... syn [javascript] (function () {window ['xr '] = {}; var midContainer = new Array (); var mapContainer = new Array (); var MAPID = 0; function Map (mid) {var type = typeof (mid); if (type! = "String") & (type! = "Number") {throw "Map id must be a string or number! ";}For (var _ c = 0; midContainer [_ c]; _ c ++) {if (mid = midContainer [_ c]) throw "You have already created Map:" + mid;} var identify = MAPID ++; midContainer [identify] = mid; mapContainer [identify] = {}; mapContainer [identify] ["id"] = mid; this. id = mid; this. prefix = "K _"; this. toString = function () {return "This is a map object! ";}} Map. prototype. getMapId = function () {return this. id;} Map. prototype. getMapIndex = function () {var index =-1; for (var _ I = 0; mapContainer [_ I]; _ I ++) {if (this. id = mapContainer [_ I] ["id"]) {index = _ I;} return index;} Map. prototype. put = function (key, value) {if (typeof (key )! = "String") {throw "key must be a string! ";} If (typeof (value) =" function ") {throw" value shouldn't be a function! ";} If (this. trimStr (key) =" ") {throw" key is empty! ";} Var index =-1; index = this. getMapIndex (); if (index! =-1) {key = this. prefix + key; mapContainer [index] [key] = value ;}} Map. prototype. get = function (key) {var index =-1; index = this. getMapIndex (); var value = ""; if (index! =-1) {var _ TV = mapContainer [index]; key = this. prefix + key; value = (_ TV. hasOwnProperty (key ))? _ TV [key]: "You haven't save this key's value! ";} Else {value =" Current Map has lost connection! ";} Return value;} Map. prototype. deleteKey = function (key) {var index =-1; index = this. getMapIndex (); key = this. prefix + key; var _ TV = mapContainer [index]; if (_ TV. hasOwnProperty (key) {delete _ TV [key] ;}} Map. prototype. clearMap = function () {var index =-1; index = this. getMapIndex (); var maxId = MAPID-1; if (index <= maxId) {for (var t = index; t <maxId; t ++) {mapContainer [t] = map Container [t + 1]; midContainer [t] = midContainer [t + 1];} mapContainer [maxId] = null; midContainer [maxId] = null; this. id = null; this. toString = null; MAPID -- ;}} Map. prototype. trimStr = function (str) {return str. replace (/(^ \ s *) | (\ s * $)/g, "");} Map. prototype. isEmpty = function () {var index =-1; index = this. getMapIndex (); if (index! =-1) {for (var attr in mapContainer [index]) {// alert (mapContainer [index] [attr]); if (attr! = "Id") {return false ;}} return true;} Map. prototype. showMap = function () {var index =-1; index = this. getMapIndex (); var str = ""; if (this. id! = Null) {str = "Map: \ t" + this. id + "\ n"; for (var attr in mapContainer [index]) {if (attr! = "Id") {str + = attr + ": \ t" + mapContainer [index] [attr] + "\ n ";}}} else {str = "This Map doesn' t exist! ";}Alert (str); return str;} window ['xr '] ['map'] = Map;}) () // test codes ---------- function test () {with (XR) {var m1 = new Map ("ddd"); m1.put ("dd", "dfdfdf"); m1.put ("dd2", "8 yeah! "); // Alert (m1.get (" dd "); // alert (m1.isEmpty (); m1.deleteKey (" dd "); m1.clearMap (); m1.showMap () ;}} test ();//-----------------