JS 自己实现Map

   function MyMap() {
        var items = {};

        this.has = function (key) {
            return key in items;
        };
        this.set = function (key, value) {
            items[key] = value;
        };
        this.remove = function (key) {
            if (this.has(key)) {
                delete items[key];
                return true;
            }
            return false;
        };
        this.get = function (key) {
            return this.has(key) ? items[key] : undefined;
        };
        this.values = function () {
            var values = new Array();
            for (var k in items) {
                if (this.has(k)) {
                    values.push(items[k]);
                }
            }
            return values;
        };
        this.getItems = function () {
            return items;
        };
        this.clear = function () {
            items = {};
        };
        this.getCount = function () {
            return Object.keys(items).length;
        };

 
    }  

 

posted @ 2018-03-15 14:13  b̶i̶n̶g̶.̶  阅读(372)  评论(0编辑  收藏  举报