[JS] Simple map implementation (normal and jquery version)

Source: Internet
Author: User
Tags imap

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

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.