美文网首页
分享一个自己写的javascript 的Map

分享一个自己写的javascript 的Map

作者: 喂夜 | 来源:发表于2017-01-07 20:00 被阅读0次

在一些低版本的浏览器中是不支持Map对象的,所以就梦想自己写一个出来,有些时候Map要比数组好用。

/**
* Created by 李志超 on 2017/1/7.
*
*/
var Map = function () {
var keySet = [];
this.size = function () {
return keySet.length;
};
this.keys = function () {
return keySet;
};
this.get = function (key) {
return this[key];
};
this.put = function (key, val) {
this[key] = val;
keySet.push(key);
return this;
};
this.remove = function (key) {
var index = keySet.indexOf(key);
if (index >= 0) {
keySet.splice(index, 1);
delete this[key];
}
return this;
};
this.clear = function () {
keySet = [];
return this;
}
};

var map = new Map();

相关文章

网友评论

      本文标题:分享一个自己写的javascript 的Map

      本文链接:https://www.haomeiwen.com/subject/gabfbttx.html