js中MAP集合的构造

/*
* MAP构造
*/
var Map = function() {
this.keys = new Array();
this.data = new Object();
this.put = function(key, val) {
if (this.data[key] == null) {
this.keys.push(key);
}
this.data[key] = val;
};
this.get = function(key) {
return this.data[key];
};
this.remove = function(key) {
this.keys.remove(key);
this.data[key] = null;
};
this.each = function(fn) {
if (typeof fn != 'function') {
return;
}
var len = this.keys.length;
for (var i = 0; i < len; i++) {
var k = this.keys[i];
fn(k, this.data[k], i);
}
};
this.entrys = function() {
var len = this.keys.length;
var entrys = new Array(len);
for (var i = 0; i < len; i++) {
entrys[i] = {
key: this.keys[i],
value: this.data[i]
}
}
};
this.isEmpty = function() {
return this.keys.length == 0;
};
this.clear = function() {
this.keys = [];
this.data = {};
}
this.size = function() {
return this.keys.length;
};
this.toString = function() {
var s = "{";
for (var i = 0; i < this.keys.length; i++, s += ",") {
var k = this.keys[i];
s += k + ":" + this.data[k];
}
s += "}";
return s;
};
}

你可能感兴趣的:(JavaScript,jquery,html5)