日期:2014-05-16  浏览次数:20467 次

js 实现map
function Map() {  
    function struct(key, value) {  
        this.key = key;  
        this.value = value;  
    }  
    this.put = function(key, value) {  
        for (var i = 0; i < this.arr.length; i++) {  
            if (this.arr[i].key === key) {  
                this.arr[i].value = value;  
                return;  
            }  
        }  
        this.arr[this.arr.length] = new struct(key, value);  
    }  
    this.get = function(key) {  
        for (var i = 0; i < this.arr.length; i++) {  
            if (this.arr[i].key === key) {  
                return this.arr[i].value;  
            }  
        }  
        return null;  
    }  
    this.remove = function(key) {  
        var v;  
        for (var i = 0; i < this.arr.length; i++) {  
            v = this.arr.pop();  
            if (v.key === key) {  
                continue;  
            }  
            this.arr.unshift(v);  
        }  
    }  
    this.size = function() {  
        return this.arr.length;  
    }  
    this.isEmpty = function() {  
        return this.arr.length <= 0;  
    }  
    this.containsKey = function(key) {  
        for (var i = 0; i < this.arr.length; i++) {  
            if (this.arr[i].key === key) {  
                return true;  
            }  
        }  
        return false;  
    }  
    this.values = function() {  
        var valueArr = new Array();  
        for (var i = 0; i