js实现简单的有序map

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/themagickeyjianan/article/details/85608721
function SortMap(){
    this._map = {};
}

SortMap.prototype.add = function(key, value){
    this._map[key] = value;
}

SortMap.prototype.get = function(key){
    return this._map[key];
}

SortMap.prototype.printInfo = function(){
    for(var key in this._map){
        console.log(this._map[key]);
    }
}

var mapObj = new SortMap();
mapObj.add("0", "JIANAN");
mapObj.add("1", "bobo");
mapObj.add("2", "xixi");
mapObj.add("3", "xiaoming");
mapObj.add("4", "lili");

mapObj.printInfo();

console.log(mapObj.get("1"));
console.log(mapObj.get("10"));

/**
JIANAN
bobo
xixi
xiaoming
lili
bobo
undefined
 */

猜你喜欢

转载自blog.csdn.net/themagickeyjianan/article/details/85608721