javascript 哈希表(hashtable)的簡(jiǎn)單實(shí)現(xiàn)
更新時(shí)間:2010年01月20日 01:10:12 作者:
javascript中沒(méi)有像c#,java那樣的哈希表(hashtable)的實(shí)現(xiàn)。在js中,object屬性的實(shí)現(xiàn)就是hash表,因此只要在object上封裝點(diǎn)方法,簡(jiǎn)單的使用obejct管理屬性的方法就可以實(shí)現(xiàn)簡(jiǎn)單高效的hashtable。
首先簡(jiǎn)單的介紹關(guān)于屬性的一些方法:
屬性的枚舉:
for/in循環(huán)是遍歷對(duì)象屬性的方法。如
var obj = {
name : 'obj1',
age : 20,
height : '176cm'
}
var str = '';
for(var name in obj)
{
str += name + ':' + obj[name] + '\n';
}
alert(str);
輸出為:name:obj1
age:20
height:176cm
檢查屬性是否存在:
in運(yùn)算符可以用來(lái)測(cè)試一個(gè)屬性是否存在。
this.containsKey = function ( key )
{
return (key in entry);
}
刪除屬性
使用delete運(yùn)算符來(lái)刪除一個(gè)對(duì)象的屬性。使用delete刪除的屬性,for/in將不會(huì)枚舉該屬性,并且in運(yùn)算符也不會(huì)檢測(cè)到該屬性。
delete entry[key];
delete obj.name;
下面是哈希表(hashtable)的js的實(shí)現(xiàn)方法:
function HashTable()
{
var size = 0;
var entry = new Object();
this.add = function (key , value)
{
if(!this.containsKey(key))
{
size ++ ;
}
entry[key] = value;
}
this.getValue = function (key)
{
return this.containsKey(key) ? entry[key] : null;
}
this.remove = function ( key )
{
if( this.containsKey(key) && ( delete entry[key] ) )
{
size --;
}
}
this.containsKey = function ( key )
{
return (key in entry);
}
this.containsValue = function ( value )
{
for(var prop in entry)
{
if(entry[prop] == value)
{
return true;
}
}
return false;
}
this.getValues = function ()
{
var values = new Array();
for(var prop in entry)
{
values.push(entry[prop]);
}
return values;
}
this.getKeys = function ()
{
var keys = new Array();
for(var prop in entry)
{
keys.push(prop);
}
return keys;
}
this.getSize = function ()
{
return size;
}
this.clear = function ()
{
size = 0;
entry = new Object();
}
}
測(cè)試:
代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HashTable</title>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/HashTable.js"></script>
<script type="text/javascript">
function MyObject(name)
{
this.name = name;
this.toString = function(){
return this.name;
}
}
$(function(){
var map = new HashTable();
map.add("A","1");
map.add("B","2");
map.add("A","5");
map.add("C","3");
map.add("A","4");
var arrayKey = new Array("1","2","3","4");
var arrayValue = new Array("A","B","C","D");
map.add(arrayKey,arrayValue);
var value = map.getValue(arrayKey);
var object1 = new MyObject("小4");
var object2 = new MyObject("小5");
map.add(object1,"小4");
map.add(object2,"小5");
$('#console').html(map.getKeys().join('|') + '<br>');
})
</script>
</head>
<body>
<div id="console"></div>
</body>
</html>
javascript hashtable實(shí)現(xiàn)代碼
http://chabaoo.cn/article/20372.htm
屬性的枚舉:
for/in循環(huán)是遍歷對(duì)象屬性的方法。如
復(fù)制代碼 代碼如下:
var obj = {
name : 'obj1',
age : 20,
height : '176cm'
}
var str = '';
for(var name in obj)
{
str += name + ':' + obj[name] + '\n';
}
alert(str);
輸出為:name:obj1
age:20
height:176cm
檢查屬性是否存在:
in運(yùn)算符可以用來(lái)測(cè)試一個(gè)屬性是否存在。
復(fù)制代碼 代碼如下:
this.containsKey = function ( key )
{
return (key in entry);
}
刪除屬性
使用delete運(yùn)算符來(lái)刪除一個(gè)對(duì)象的屬性。使用delete刪除的屬性,for/in將不會(huì)枚舉該屬性,并且in運(yùn)算符也不會(huì)檢測(cè)到該屬性。
delete entry[key];
delete obj.name;
下面是哈希表(hashtable)的js的實(shí)現(xiàn)方法:
復(fù)制代碼 代碼如下:
function HashTable()
{
var size = 0;
var entry = new Object();
this.add = function (key , value)
{
if(!this.containsKey(key))
{
size ++ ;
}
entry[key] = value;
}
this.getValue = function (key)
{
return this.containsKey(key) ? entry[key] : null;
}
this.remove = function ( key )
{
if( this.containsKey(key) && ( delete entry[key] ) )
{
size --;
}
}
this.containsKey = function ( key )
{
return (key in entry);
}
this.containsValue = function ( value )
{
for(var prop in entry)
{
if(entry[prop] == value)
{
return true;
}
}
return false;
}
this.getValues = function ()
{
var values = new Array();
for(var prop in entry)
{
values.push(entry[prop]);
}
return values;
}
this.getKeys = function ()
{
var keys = new Array();
for(var prop in entry)
{
keys.push(prop);
}
return keys;
}
this.getSize = function ()
{
return size;
}
this.clear = function ()
{
size = 0;
entry = new Object();
}
}
測(cè)試:
代碼
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HashTable</title>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/HashTable.js"></script>
<script type="text/javascript">
function MyObject(name)
{
this.name = name;
this.toString = function(){
return this.name;
}
}
$(function(){
var map = new HashTable();
map.add("A","1");
map.add("B","2");
map.add("A","5");
map.add("C","3");
map.add("A","4");
var arrayKey = new Array("1","2","3","4");
var arrayValue = new Array("A","B","C","D");
map.add(arrayKey,arrayValue);
var value = map.getValue(arrayKey);
var object1 = new MyObject("小4");
var object2 = new MyObject("小5");
map.add(object1,"小4");
map.add(object2,"小5");
$('#console').html(map.getKeys().join('|') + '<br>');
})
</script>
</head>
<body>
<div id="console"></div>
</body>
</html>
javascript hashtable實(shí)現(xiàn)代碼
http://chabaoo.cn/article/20372.htm
相關(guān)文章
js倒計(jì)時(shí)簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了js倒計(jì)時(shí)簡(jiǎn)單實(shí)現(xiàn)方法,方便一些提示重要日期的來(lái)臨,感興趣的小伙伴們可以參考一下2015-12-12js+html+css實(shí)現(xiàn)手動(dòng)輪播和自動(dòng)輪播
這篇文章主要為大家詳細(xì)介紹了js+html+css實(shí)現(xiàn)手動(dòng)輪播和自動(dòng)輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12獲取DOM對(duì)象的幾種擴(kuò)展及簡(jiǎn)寫(xiě)
獲取DOM對(duì)象的幾種擴(kuò)展及簡(jiǎn)寫(xiě)...2006-10-10javascript使用appendChild追加節(jié)點(diǎn)實(shí)例
這篇文章主要介紹了javascript使用appendChild追加節(jié)點(diǎn)的方法,實(shí)例分析了appendChild()函數(shù)增加結(jié)點(diǎn)的使用技巧,需要的朋友可以參考下2015-01-01微信小程序點(diǎn)餐系統(tǒng)開(kāi)發(fā)常見(jiàn)問(wèn)題匯總
這篇文章主要介紹了微信小程序點(diǎn)餐系統(tǒng)開(kāi)發(fā)常見(jiàn)問(wèn)題匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08JavaScript用JSONP跨域請(qǐng)求數(shù)據(jù)實(shí)例詳解
Javascript跨域訪(fǎng)問(wèn)是web開(kāi)發(fā)者經(jīng)常遇到的問(wèn)題,什么是跨域,就是一個(gè)域上加載的腳本獲取或操作另一個(gè)域上的文檔屬性。下面這篇文章主要介紹了JavaScript用JSONP跨域請(qǐng)求數(shù)據(jù)的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01