• js模拟map集合


    /** Map 用法.           //AM805 s
    *  var map = new Map();
    * map.put(" /. ][>?,国a", "aaa");
    * map.put("b","bbb");
    * map.put("cc","cccc");
    * map.put("'/doc/xab[2]/test[1]'","ccc");
    * map.remove("cc");
    * var array = map.keySet();
    * for(var i in array) {
    * document.write("key:(" + array[i] +") <br>value: ("+map.get(array[i])+") <br>");
    * }
    */
    
    /**
    * Map的构造函数
    */
    function Map(){
        this.container = new Object();
    }
    
    /**
    * 添加一个键-值
    */
    Map.prototype.put = function(key, value){
        this.container[key] = value;
    }
    
    /**
    * 通过键获取一个值
    */
    Map.prototype.get = function(key){
        return this.container[key];
    }
    
    /**
    * 返回该Map对象的键集合
    */
    Map.prototype.keySet = function() {
        var keyset = new Array();
        var count = 0;
        for (var key in this.container) {
            // 跳过object的extend函数
            if (key == 'extend') {
                continue;
            }
            keyset[count] = key;
            count++;
        }
        return keyset;
    }
    
    /**
    * 返回该Map对象的大小
    */
    Map.prototype.size = function() {
        var count = 0;
        for (var key in this.container) {
            // 跳过object的extend函数
            if (key == 'extend'){
            continue;
            }
            count++;
        }
        return count;
    }
    
    /**
    * 删除Map对象
    */
    Map.prototype.remove = function(key) {
    delete this.container[key];
    }
    
    /**
    * 返回该Map对象的字符串形式
    */
    Map.prototype.toString = function(){
        var str = "";
        for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
            str = str + keys[i] + "=" + this.container[keys[i]] + ";
    ";
        }
        return str;
    }
  • 相关阅读:
    final关键字
    海思NB-IOT的SDK看门狗的使用
    IAR环境下编译CC2640入门开发
    股票操作记录180613(2)
    股票操作笔记18年6月13(1)
    PyYAML学习第一篇
    片仔癀犯过的错误
    2018年5月份片仔癀最佳演员奖
    2018-05-22两只垃圾基金南方产业活力000955和鹏华全球高收益债券000290
    linux c编程:网络编程
  • 原文地址:https://www.cnblogs.com/gavenQin/p/5577447.html
Copyright © 2020-2023  润新知