自己项目中用到了这个,记录下来,以便下次不用到处找
//***************************** Map javascript **************************
function Map(linkItems) {
this.current = undefined;
this._size = 0;
if(linkItems === false){
this.disableLinking();
}
}
/**
* get currently map
* @return current object
*/
Map.noop = function() {
return this;
};
/**
* invalid operation
* @return
*/
Map.illegal = function() {
throw new Error('invalid operation, the Map has been disable');
};
/**
*
* @param obj
* @param foreignKeys
* @return
*/
Map.from = function(obj, foreignKeys) {
var map = new Map;
for(var prop in obj) {
if(foreignKeys || obj.hasOwnProperty(prop)){
map.put(prop, obj[prop]);
}
}
return map;
};
/**
* disable map
* @return
*/
Map.prototype.disableLinking = function() {
this.link = Map.noop;
this.unlink = Map.noop;
this.disableLinking = Map.noop;
this.next = Map.illegal;
this.key = Map.illegal;
this.value = Map.illegal;
this.clear = Map.illegal;
return this;
};
/**
* return hash value eg: number 123
* @param value key/value
* @return
*/
Map.prototype.hash = function(value) {
return (typeof value) + ' ' + (value instanceof Object ? (value.__hash || (value.__hash = ++arguments.callee.current)) : value.toString());
};
/**
* return map length
* @return
*/
Map.prototype.size = function() {
return this._size;
};
Map.prototype.hash.current = 0;
/**
* get value by key
* @param key
* @return
*/
Map.prototype.get = function(key) {
var item = this[this.hash(key)];
return item === undefined ? undefined : item.value;
};
/**
* push the value into map
* @param key
* @param value
* @return
*/
Map.prototype.put = function(key, value) {
var hash = this.hash(key);
if(this[hash] === undefined) {
var item = { key : key, value : value };
this[hash] = item;
this.link(item);
++this._size;
}else{
this[hash].value = value;
}
return this;
};
/**
* delete value by key
* @param key
* @return
*/
Map.prototype.remove = function(key) {
var hash = this.hash(key);
var item = this[hash];
if(item !== undefined) {
--this._size;
this.unlink(item);
delete this[hash];
}
return this;
};
/**
* clear map
* @return
*/
Map.prototype.clear = function() {
while(this._size){
this.remove(this.key());
}
return this;
};
/**
* queue
* @param item
* @return
*/
Map.prototype.link = function(item) {
if(this._size == 0) {
item.prev = item;
item.next = item;
this.current = item;
}else {
item.prev = this.current.prev;
item.prev.next = item;
item.next = this.current;
this.current.prev = item;
}
};
Map.prototype.unlink = function(item) {
if(this._size == 0){
this.current = undefined;
}else {
item.prev.next = item.next;
item.next.prev = item.prev;
if(item === this.current){
this.current = item.next;
}
}
};
/**
* get next
* @return
*/
Map.prototype.next = function() {
this.current = this.current.next;
return this;
};
/**
* get current key
* @return
*/
Map.prototype.key = function() {
return this.current.key;
};
/**
* get current value
* @return
*/
Map.prototype.value = function() {
return this.current.value;
};
//***************************** Map javascript **************************
分享到:
相关推荐
原创 在javascript中,仅提供了数组(Array)对象,本文利用数组对象来模式实现MAP对象
js Map 对象,js map ,map ,键值对
javascript 模拟 java中的 List,Map js文件为 js/utils.js IE6.0 测试通过 <br>List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 remove...
* Map对象,实现map功能 * put(key, value) 向map中增加键值对 * 例子: * var map = new Map(); * * map.put("key", "value"); * var val = map.get("key");
里面包含两个实例,js实现map的相关功能
使用js实现map工具类
里面有三个js文件,都是需要引入的。具体的方法,可以看源码。
本篇文章给大家介绍javascript中实现map的示例代码,代码写的简单易懂,可以获取、删除键值,刚兴趣的朋友跟着脚本之家小编一起学习吧。
部分IE浏览器不支持ecmascript 6,无法使用map对象,用原生js对象实现map功能
javascript实现map,极其方便,可在需要当前页处理多数据时使用
主要介绍了JavaScript中实现map功能代码分享,本文直接给出实现代码,给出一个MAP类,类中包含大多数的常用方法,还可以自己扩展,需要的朋友可以参考下
自己设计的用JavaScript实现Array 和Map 里面有三个Js文件,都需要引入.
通过JS的使用,实现类似于JAVA中Map的操作
javascript map类。类中有相应demo,在javascript中实现Map的一个类,包括map中对应的一些数据结构相关算法。
主要介绍了JavaScript中利用Array和Object实现Map的方法,实例分析了javascript实现map的添加、获取、移除、清空、遍历等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://hoochiang.iteye.com/blog/1688307
高性能的 javascript map实现,稳定性强的 基于 JSON的map! *同时附赠网络上搜索的map一个,用于对比。 经过笔者测试,网络上的map不如笔者所写的map性能高,容量也相差很多。
NULL 博文链接:https://minjiechenjava.iteye.com/blog/2008167