Common methods for simulating the collection class map of Java in JavaScript

Source: Internet
Author: User

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

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.