文章目录
  1. 1. 实现JS的Map类

实现JS的Map类

两种方式:

  • 简单的Object
function Map(){}
Map.prototype.get = function(key){
    return this[key];
};
Map.prototype.set = function(key,val){
    this[key]=val;
};

// var m = new Map();
// m.set('name','foo');
// console.log(stringify(m)); // {"name":"foo"}
  • 实现map的缓存功能

UtilMap.js:

/**
 *	An object that maps keys to values. A map cannot contain duplicate keys;
 *	each key can map to at most one value.
 *	@author	Todd Ditchendorf
 */
Map = function (o) {
/*	if (o && !(o instanceof Object)) {
        throw new Error("IllegalArgumentException: Map's " +
                    "constructor's only argument must be an Object");
    }*/
    this._obj = (o) ? (o) : new Object();
};

/**
 *	Associates the specified value with the specified key in this map.
 *	@param	Object key	New key to enter,
 *	@param	Object Value	New value to enter.
 *	@returns Object
 */
Map.prototype.put = function (key,value) {
    this._obj[key] = value;
};

/**
 *	Returns the value to which this map maps the specified key.
 *	@param	Object key	Get the value for this key in this Map.
 *	@returns Object
 */
Map.prototype.get = function (key) {
    if (!this._obj[key]) return null;
    return this._obj[key];
};

/**
 *	Returns the number of key-value mappings in this map.
 *	@returns number
 */
Map.prototype.size = function () {
    var count = 0;
    for (var key in this._obj)
        count++;
    return count;
};

/**
 *	Returns true if this map contains no key-value mappings.
 *	@returns	boolean
 */
Map.prototype.isEmpty = function () {
    return this.size() == 0;
};

/**
 *	Returns a string representation of this map.
 *	@returns String
 */
Map.prototype.toString = function () {
    var buff = new StringBuffer();
    count = 0;
    for ( var key in this._obj ) {
        buff.append(key).append(" = ").append(this._obj[key])
            .append(count == this.size() - 1 ? "" : "\r\n");
        count++;
    }
    return buff.toString();
};

/**
 *	Returns true if this map contains a mapping for the specified  key.
 *	@param	Object	key	Testing this Map for key with this value.
 *	@returns	boolean
 */
Map.prototype.containsKey = function (key) {
    if (this._obj[key] !== undefined) {
        return true;
    }
    return false;
};

/**
 *	Returns true if this map maps one or more keys to the  specified value.
 *	@param	Object	value	Testing this Map for key with this value.
 *	@returns	boolean
 */
Map.prototype.containsValue = function (value) {
    for (var key in this._obj) {
        if (this._obj[key] == value) {
            return true;
        }
    }
    return false;
};

/**
 *	Removes the mapping for this key from this map if it is present
 *	@param	Object	key	Testing this Map for key with this value.
 *	@returns	boolean
 */
Map.prototype.remove = function (key) {
    if (this.containsKey(key)) {
        delete this._obj[key];
    }
};

/**
 *	Returns a set view of the values contained in this map.
 *	@returns	Array
 */
Map.prototype.keySet = function () {
    var keys = [];
    for (var key in this._obj) {
        keys.push(key);
    }
    return keys;
};

/**
 *	Returns a collection view of the values contained in this map.
 *	@returns	Collection
 */
Map.prototype.values = function () {
    var values = new Collection();
    for (var key in this._obj) {
        values.add(this._obj[key]);
    }
    return values;
};

很多自以为nodejs就直接支持map,或者说js直接就直接map,那是不完全对的,应该说是对json对象的支持很好。。。。。。

文章目录
  1. 1. 实现JS的Map类