A JavaScript hash class

Source: Internet
Author: User
Javascript

<script type= "Text/javascript" >
The source code is as follows:
var Hash = function () {
This._data = new Object ();
}
function Hash$add (key,value) {
if (!key) return;
if (typeof (value) = = ' undefined ') return;
This._data[key] = value;
}
function Hash$addrange (h) {
if (!h | |!h._data) return;
for (var key in H._data) {
var value = H._data[key];
This.add (Key,value);
}
}
function Hash$remove (key) {
if (!key) return null;
var value = This._data[key];
Delete This._data[key];
return value;
}

function Hash$removeat (index) {
if (isNaN (index)) return null;
var i = 0;
for (var key in This._data) {
if (i = = index) {
return This.remove (key);
}
i++;
}
return null;
}
function Hash$removerange (startindex,endindex) {
if (isNaN (startIndex) | | | | isnan (ENDINDEX)) return null;
if (StartIndex > Endindex) return null;
var i = 0;
var h = new Hash ();
for (var key in This._data) {
if (I>=startindex && i<= endindex) {
H.add (Key,this.remove (key));
}else if (i>endindex) {
Break
}
i++;
}
return h;
}
function Hash$getcount () {
var i = 0;
for (var key in This._data) i++;
return i;
}
function Hash$find (key) {
if (!key) return null;
return This._data[key];
}
function Hash$foreach (method,instance) {
var i = 0;
for (var key in This._data) {
var value = This._data[key];
if (typeof (value)!== ' undefined ') {
var r = Method.call (instance, key, value, I, this);
if (r = = "Break") break;
i++;
}
}
}
function Hash$getkeys () {
var arr = new Array ();
for (var key in This._data) {
Arr.push (key);
}
return arr;
}
function Hash$getvalues () {
var arr = new Array ();
for (var key in This._data) {
Arr.push (This._data[key]);
}
return arr;
}
Hash.prototype = {
_data:null,
Add:hash$add,
Addrange:hash$addrange,
Remove:hash$remove,
Removeat:hash$removeat,
Removerange:hash$removerange,
Getcount:hash$getcount,
Find:hash$find,
Foreach:hash$foreach,
Getkeys:hash$getkeys,
Getvalues:hash$getvalues
}
Hash.__typename = ' Hash ';


//test code as follows:
//test Hash class
var h1 = new Hash ();
H1.add ("FSD", "1");
H1.add ("Xdfs", "22");
Alert (H1.getcount ());//2

var h2 = new Hash ();
H1.foreach (function (key,value,i,h) {
    this.add (key,value);   //H2, H1
    h.remove (key); 
},h2);
Alert (H1.getcount () + "," +h2.getcount ());//0,2

var h3 = new Hash ();
H3.addrange (H2);
Alert (H3.getcount ());//2

var ks = H3.getkeys ();
for (i=0 i<ks.length; i++) {
    alert (ks[i]+ ": +h3.find (Ks[i)));   
}
var vs = H3.getvalues ();
for (i=0; i<vs.length; i++) {
    alert (vs[i]);
}

H3.add ("FSA", "333");
H3.add ("FSA", "4444");//This will only add one, which will later overwrite
alert (H3.getcount ()) with the value of the previous key with the same name;//3 instead of 4

var h4 = H3.removerange (0,2);
Alert (H4.getcount ());
H4.foreach (function (k,v,i,h) {
if (v = = "") return "break";//You can use the back ' break ' to jump out of the loop body
Alert (k + ":" +v+ ":" +i);
});
</script>



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.