准备工作:
1. MAP对象的自定义基本方法:
- keys : new Array();
- data : new Array();
- seKeyValue(key,value);
- remove(value);
- clear();
2. 由于javascript的Array()原生对象中没有remove方法。所以我也新建了一个数组对象,在数组对象中完成remove方法,在在map对象中继承数组对象,完成Map的remove方法


  • CapArr:

    /*Array 对象*/
    
    var CapArr = function(){
    var arr = new Array();  
    var init = function(arr){
        if(arr!=undefined){
            this.arr = arr; 
        }
        return this.arr;
    };
    /**
     * 相同长度数组元素相加
     * @param arr1
     * @param arr2
     * @returns
     */
    var addValues = function(arr1,arr2){        
        if(arr1.length!=arr2.length){
            return ;
        }
        for(var i=0;i<arr1.length;i++){
            arr1[i]=arr2[i]+arr1[i];
        }       
        return arr1;
    };
    /**
     * 数组判断是或否含有某元素
     */
    var contains = function(ele,arr){
        var layer = new CapLayer();
        var rel = false;
        if(arr!=undefined&&arr.length>0){
            $.each(arr,function(i,obj){
                if(obj==ele){
                    rel = true;
                    return rel;
                }
            });
            return rel;
        }else{
            layer.msgWin("参数错误");
        }
    
    }; 
    /* 获取数组中元素下标,没有的话返回-1 */
    var indexOfElementInArray = function(val,array) {              
            for (var i = 0; i < array.length; i++) {  
                if (array[i] == val) return i;  
            }  
            return -1;  
        };
    /* 根据下标删除数组中指定位置的元素*/   
    var removeElementInArray = function(val,array) {  
        var index = this.indexOfElementInArray(val,array);  
        if (index > -1) {  
            array.splice(index, 1);         
        }  
    }; 
    this.addValues      = addValues;
    this.arr            = arr;
    this.init           = init;
    this.contains       = contains;
    this.indexOfElementInArray = indexOfElementInArray;
    this.removeElementInArray = removeElementInArray;
    };
  • CapMap:
/*-Map集合对象-*/
/**
 * Map<key,data>
 * <可自定义Key>
 */
var CapMap = function(){
        var keys = new Array();
        var data = new Array(); 
        var clear   = function(){
            this.keys = new Array();
            this.data = new Array();
        };
        /**
         * 更新key对应的Value属性,若不存在,添加此Key
         * @param key
         * @param value
         */
        var setKeyValue = function(key, value) {
            if (this.data[key] == null) {// 如键不存在则身【键】数组添加键名
                this.keys.push(key);
                }       
            this.data[key] = value;// 给键赋值
        };

        var getValueByKey = function(key) {
            return this.data[key];
        };

        var setKeys = function(keyArray){
            if(typeof(keyArray)!='Array'){
                alert("setKeys must insert Object as Array()");
                return ;
            }
            this.keys=keyArray;
        };

        var setValues = function(dataArray){
            if(typeof(dataArray)!='Array'){
                alert("setValues must insert Object as Array()");
                return ;
            }
            this.data = dataArray;
        };
        /*此处的继承其实更像是将CapArr对象的引用指给了c,这样c就也能使用CapArr对象中的方法了,
        区别就是c可以拥有自己另外的属性,即继承的扩展*/
        var remove = function(key) {
            var c = {};
             Object.setPrototypeOf(c, new CapArr());
            c.removeElementInArray(key,this.keys);
            c.removeElementInArray(data[key],this.data);            
        };

        var isEmpty = function() {
            return this.keys.length == 0;
        };

        var size = function() {
            return this.keys.length;
        };
        this.data = data;
        this.keys = keys;
        this.init = init;
        this.clear = clear;
        this.setKeyValue = setKeyValue;
        this.getValueByKey = getValueByKey;
        this.setKeys = setKeys;
        this.setValues = setValues;
        this.remove = remove;
        this.isEmpty = isEmpty;
        this.size = size;   
};