javascript仿es6的map类

2018-06-24 01:46:23来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 var Dictionary = (function (){
        var f = function(){
            this._items = {};
        };
        var proto = f.prototype;
        proto.has = function(key){
            return key in this._items;
        }
        proto.set = function(key,value){
            this._items[key] = value;
        }
        proto.remove = function(key){
            if(this.has(key)){
                delete this._items[key];
                return true;
            }
            return false;
        }
        proto.get = function(key){
            return this.has(key) ? this._items[key] : undefined;
        };
        proto.values  = function (){
            var values = [];
            for(var k in this._items){
                if(this.hsa(k)){
                    values.push(this._items[k]);
                }
            }
            return values;
        };
        proto.clear = function(){
            this._items = {};
        };
        proto.size = function(){
            var count = 0;
            for(var prop in this._items){
                if(this._items.hasOwnProperty(prop)){
                     ++ count;
                }
            }
            return count;
        };
        proto.keys = function(){
            var keys = [];
            for(var prop in this._items){
                if(this._items.hasOwnProperty(prop)){
                    keys.push(prop);
                }
            }
            return keys;
        };
        proto.getItems = function(){
            return this._items;
        }
        return f;
    })();

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:详解Javascript中prototype属性(推荐)

下一篇:浅谈JavaScript、ES5、ES6