<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 实现一个简单的HashTable </TITLE>
</HEAD><BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
function AlertValue(s){
   document.write(s + "<br/>");
}//简单hash类型
function HashTable(){
   //特殊关键字(speacialKey)用来处理特殊的保留字
   //这些保留字主要是Object对象中的固有属性和方法
   var speacialKey = [
    "valueOf",
    "hasOwnProperty",
    "isPropertyOf",
    "propertyIsEnumerable",
    "prototype",
    "constructor",
    "toLocaleString",
    "toString" 
   ];
   //为关键字单独提供存放值空间
   var speacialvalue = new Array(speacialKey.length);
   //特殊关键字存储标记,true表示位置已经有值不能重复插入
   var speacialflag = new Array(speacialKey.length);
   //存放普通关键字,只需一个普通对象
   var normalHashtable = {};   //将值插入hash表
   this.insert = function(key,value){
    //处理特殊关键字
    for(var i =0;i<speacialKey.length;i++){
     if(key == speacialKey[i]){
      if(!speacialflag[i]){
       speacialvalue[i] = value;
       speacialflag[i] = true;
       return true
      }else{
       return false;
      }
     }
    }
    //处理普通关键字
    normalHashtable[key] = value;
    return true;
   }
   //在hash中查找相关值
   this.find = function(key){
  
    //先处理特殊关键字
    for(var i=0;i<speacialKey.length;i++){
     if(key == speacialKey[i]){    
      return speacialvalue[i];
     }
    }
    //查找普通关键字
    return normalHashtable[key];
   }
}var hash = new HashTable();
AlertValue(hash.insert("prototype","idea"));
AlertValue(hash.insert("prototype","idea"));
AlertValue(hash.find("prototype"));
//-->
</SCRIPT></BODY>
</HTML>