javascript hash

<script type="text/javascript">
//源码如下:
var Hash = function(){
    this._data = new Object();   
}
    function Hash$add(key,value){    
        if(!key) return;
        if(typeof(value) === 'undefined') return;       
        this._data[key] = value;  
    }
    function Hash$addRange(h){
        if(!h || !h._data) return;
        for(var key in h._data){
            var value = h._data[key];           
            this.add(key,value);           
        }
    }
    function Hash$remove(key){
        if(!key) return null;
        var value = this._data[key];
        delete this._data[key];   
        return value;             
    }
   
    function Hash$removeAt(index){
        if(isNaN(index)) return null;
        var i = 0;       
        for(var key in this._data){
            if(i == index){
                return this.remove(key);
            }
            i++;
        }
        return null;
    }
    function Hash$removeRange(startIndex,endIndex){
        if(isNaN(startIndex) || isNaN(endIndex)) return null;
        if(startIndex > endIndex)return null;
        var i = 0;
        var h = new Hash();
        for(var key in this._data){
            if(i>=startIndex && i<= endIndex){
                h.add(key,this.remove(key));
            }else if(i>endIndex){               
                break;
            }
            i++;
        }       
        return h;
    }   
    function Hash$getCount(){
        var i = 0;
        for(var key in this._data) i++;       
        return i;
    }
    function Hash$find(key){
        if(!key) return null;  
        return this._data[key];
    }
    function Hash$forEach(method,instance){
        var i = 0;
        for(var key in this._data){
            var value = this._data[key];
            if(typeof(value) !== 'undefined'){
                var r = method.call(instance, key, value, i, this);
                if(r == 'break') break;               
                i++;
            }
        }
    }
    function Hash$getKeys(){
        var arr = new Array();
        for (var key in this._data){
            arr.push(key);
        }
        return arr;
    }
    function Hash$getValues(){
        var arr = new Array();
        for (var key in this._data){
            arr.push(this._data[key]);
        }
        return arr;       
    }
Hash.prototype = {
    _data : null,
    add : Hash$add,
    addRange: Hash$addRange,
    remove : Hash$remove,
    removeAt : Hash$removeAt,
    removeRange : Hash$removeRange,
    getCount : Hash$getCount,
    find : Hash$find,
    forEach : Hash$forEach,
    getKeys : Hash$getKeys,
    getValues : Hash$getValues   
}
Hash.__typeName = 'Hash';


//测试代码如下:
//Test Hash class
var h1 = new Hash();
h1.add("fsd","1");
h1.add("xdfs","22");
alert(h1.getCount()); //2

var h2 = new Hash();
h1.forEach(function(key,value,i,h){
    this.add(key,value);    //h2中加,h1中减
    h.remove(key); 
},h2);
alert(h1.getCount()+","+h2.getCount());//0,2

var h3 = new Hash();
h3.addRange(h2);
alert(h3.getCount());//2

var ks = h3.getKeys();
for(i=0; i<ks.length; i++){
    alert(ks[i]+": "+h3.find(ks[i]));   
}
var vs = h3.getValues();
for(i=0; i<vs.length; i++){
    alert(vs[i]);
}

h3.add("fsa","333");
h3.add("fsa","4444");//这里只会增加一个,即后来的会把前面同名键的值覆盖掉
alert(h3.getCount());//3 而不是 4

var h4 = h3.removeRange(0,2);
alert(h4.getCount());
h4.forEach(function(k,v,i,h){
    if(v == '22') return 'break';//可以使用返回 'break' 来跳出循环体
    alert(k+": "+v+": "+i);
});
</script>


posted @ 2011-11-21 13:40  Podevor  阅读(335)  评论(0编辑  收藏  举报