JS Create map

Source: Internet
Author: User

function Map () {
var struct = function (key, value) {
This.key = key;
This.value = value;
}

var put = function (key, value) {
for (var i = 0; i < this.arr.length; i++) {
if (This.arr[i].key = = = key) {
This.arr[i].value = value;
Return
}
}
This.arr[this.arr.length] = new struct (key, value);
}

var get = function (key) {
for (var i = 0; i < this.arr.length; i++) {
if (This.arr[i].key = = = key) {
return this.arr[i].value;
}
}
return null;
}

var remove = function (key) {
var V;
for (var i = 0; i < this.arr.length; i++) {
v = this.arr.pop ();
if (V.key = = = key) {
Continue
}
This.arr.unshift (v);
}
}

var size = function () {
return this.arr.length;
}

var isEmpty = function () {
return this.arr.length <= 0;
}
This.arr = new Array ();
This.get = get;
This.put = put;
This.remove = remove;
this.size = size;
This.isempty = IsEmpty;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Untitled Document </title>
<script src= "Jquery-1.7.2.js" type= "Text/javascript" ></script>

Encapsulate the map into JS introduction
<script src= "Map.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >

function Test () {

var map = new map ();
Map.put ("Re", "Redhacker");

Alert ("Mapvalue" + map.get ("Re"))

}


</script>

<body>
<form>

<input type= "button" value= "Take map value" onclick= "Test ();"
</form>
</body>

JS Create map

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.