再学JavaScript-第二课-模拟Map

来源:互联网 发布:vim调试js 编辑:程序博客网 时间:2024/06/05 04:43
var BASE = BASE||{};BASE.Map = function () {    var _hash = {};    this._hash = _hash;    this.put = function (key,value) {        _hash[key] = value;    };    this.get = function (key) {        if(_hash[key] || _hash[key] === 0 || _hash[key]===false){    //防止 value为 0或者false 的情况   js类型自动转换            return _hash[key];        }else {            return null;        }    } ;    this.remove = function (key) {        if(_hash[key] || _hash[key] === 0 || _hash[key]===false){            delete _hash[key];        }    };    this.size = function () {        var count = 0;        for(var k in _hash){            count++;        }        return count;    };    this.toArray = function () {        var re = [];        for(var k in _hash){            re.push(_hash[k]);        }        return re;    } ;    this.each  = function (handler) {        for(var e in _hash){            handler&&handler(_hash[e]);        }    }};
0 0
原创粉丝点击