天天看点

利用javascript的prototype的继承来写自定义MAP的remove方法

准备工作:

1. MAP对象的自定义基本方法:

- keys : new Array();

- data : new Array();

- seKeyValue(key,value);

- remove(value);

- clear();

  • CapArr:
/*-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;   
};      
  • 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;   
};      

继续阅读