javascript 雜湊表(hashtable)的簡單實現

來源:互聯網
上載者:User

首先簡單的介紹關於屬性的一些方法:
屬性的枚舉:
for/in迴圈是遍曆對象屬性的方法。如 複製代碼 代碼如下: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運算子可以用來測試一個屬性是否存在。 複製代碼 代碼如下:this.containsKey = function ( key )
{
return (key in entry);
}

刪除屬性
使用delete運算子來刪除一個對象的屬性。使用delete刪除的屬性,for/in將不會枚舉該屬性,並且in運算子也不會檢測到該屬性。
delete entry[key];
delete obj.name;
下面是雜湊表(hashtable)的js的實現方法: 複製代碼 代碼如下: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();
}
}

測試:
代碼 複製代碼 代碼如下:<!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實現代碼
http://www.jb51.net/article/20372.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.