Free to write a JavaScript version of the map, you want to be able to use the least code to achieve the following features: storage Key-value key-value pairs of arrays to traverse the map
First, let's look at the normal version:
(function ($) {$.imap = function () {var IMAP = This.map = {}; this.get = function (key) {var val = eval ("IMAP." +key); Return (typeof val = = undefined)? Null:val; }; This.set = function (key,value) {eval ("IMAP.") +key+ "=value"); }; }; }) (window);
Next is the test case, hehe.
var map = new Window.imap (); Map.set (' strkey ', ' Hello World '); document.write ("The value of key ' strkey ' in map is" + map.get (' strkey ') + "<br/>"); var obj = {A: ' Z '}; Map.set (' Objkey ', obj); document.write ("The value of key ' Objkey ' in map is" + map.get (' objkey ') + "<br/>");
Iterate through all the elements of the map:
for (var ele in Map.map) {document.write ("key:" +ele + "//value:" + map.get (ele) + "<br/>");}
As long as the above window replaced by "$" is juqery version, it is convenient.
The above version is a map based on the object implementation, so there is a limit: The map key value cannot be the key that starts with the number