javascript 实现hashtable集合
来源:互联网 发布:考研网络课程哪家好 编辑:程序博客网 时间:2024/05/28 06:06
看到一篇关于js 实现hashtable 的文章http://www.blogjava.net/fantasy/archive/2006/08/02/44742.html#61363
,挺不错,考虑到现在很多网友都使用prototype开发,所以我用prototype的方式重新实现了这个hashtable 集合,并修正了一个小bug (hashtable中删除对象前先判断存在)。
//hashtable in javascript
var Collections = new Object();
Collections.Base = Class.create();
Collections.Base.prototype = {
initialize:function()
{
this.count = 0 ;
this.container = new Object();
}
}
Collections.Hashtable = Class.create();
Collections.Hashtable.prototype = Object.extend(new Collections.Base(),
{
add:function(key ,value)
{
if(!this.containsKey(key))
{
this.count++;
}
this.container[key] = value;
},
get:function(key)
{
if(this.containsKey(key))
{
return this.container[key];
}
else
{
return null;
}
},
containsKey:function(key)
{
return (key in this.container);
},
containsValue:function(value)
{
for(var prop in this.container)
{
if(this.container[prop]==value)
{
return true;
}
}
return false;
},
keys:function()
{
var keys = new Array();
for(var prop in this.container)
{
keys.push(prop);
}
return keys;
},
values:function()
{
var values = new Array();
for(var prop in this.container)
{
values.push(this.container[prop]);
}
return values;
},
remove:function()
{
if(this.containsKey(key))
{
delete this.container[key];
this.count--;
}
}
}
);
调用方式可以如下
var ht = new Collections.Hashtable();
ht.add("guest1" ,"Jackson");
ht.add("guest2" ,"Tom");
取值
var name = ht.get("guest1");
其他方法应该比较直观,不用多说了,呵呵!
- javascript 实现hashtable集合
- javascript的hashtable实现
- Javascript实现HashTable(哈希表)
- javascript实现HashTable
- javascript中的哈希表(hashtable)实现
- javascript实现HashTable(完整版)
- Java集合类Hashtable实现细节
- 【java集合】自己实现简易的Hashtable
- 集合 - HashTable
- 集合-HashTable
- 哈希表(hashtable)的javascript简单实现
- javascript 哈希表(hashtable)的简单实现
- JAVASCRIPT HashTable
- javascript hashtable
- javascript HashTable
- 深入Java集合学习系列:Hashtable的实现原理
- 深入Java集合学习系列:Hashtable的实现原理
- 深入Java集合学习系列:Hashtable的实现原理
- 权限分配程序总结(C#)
- .NET Reactor v2.3.0.5
- 我公司急需软件开发工程师一名
- 雅虎MySpace轮番遇袭 Web2.0反成黑客温床
- Windows Mobile开发环境搭建指南(转贴)
- javascript 实现hashtable集合
- 测试
- 什么是module 以及如何写一个module
- GDIPLUS
- 世界编程语言排名:Ruby崛起,狂升15位
- 黑色星期一颠覆“百度神话” 故事还能讲多久?
- 北京特色小店
- 重用ADOBE报表图形
- 商业地产管理软件