本文主要是介绍再学JavaScript-第二课-模拟Map,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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]);}}
};
这篇关于再学JavaScript-第二课-模拟Map的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!