Common methods for simulating the collection class map of Java in JavaScript
(function Map () {var size = 0, Map = {},strundefined;function MapCache () {}mapcache.prototype.put = function (key, value) {I F (key = = NULL | | key = = = undefined) {throw new Error ("null or undefined is not a illegality variable!");} if (map[key + ""] = = strundefined) {map[key + ""] = Value;size++;return;} map[key + ""] = value;} MapCache.prototype.remove = function (key) {var value;if (key!== null | | key!== strundefined) {value = map[key + "" ];d elete map[key + ""];size--;return value;} return value;} MapCache.prototype.get = function (key) {if (Map[key + ""]! = strundefined) {return map[key + ""];} return null;} MapCache.prototype.clear = function () {map = {};size = 0;} MapCache.prototype.size = function () {return size;} MapCache.prototype.keys = function () {var key, keys = [];//here because the map is declared in the form of object literals, the prototype object defaults to object//So no need to use hasOwnProperty The method filters the attributes in the prototype chain for (key in map) {Keys.push (key); } return keys; Complies with AMD Modular specification, supports REQUIREJS modular export if (typeof define = = = ' FunctiOn ' && define.amd) {define (' map ', [], function () {return mapcache;})} The export name JMap is primarily designed to prevent and remove the free Map naming conflict window in Chrome. JMap = MapCache;} ());
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common methods for simulating the collection class map of Java in JavaScript