Map toolkit for javascript implementation

Source: Internet
Author: User
Tags hasownproperty
[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 ();//-----------------
Related Article

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.