我写的是:
function save(mosn, cause){
  var map = new Object();   (1)
  var map1 = new Object();  (2)
  map.put(mosn, cause);     (3)
  for(var p in map){        
    ....
  }
}
1.(1)(2)这两句是new Object(),还是new HashMap()?
2.(3)这样写可以吗?
objecthashmapfunction

解决方案 »

  1.   

    1 ,2 就是定义的对象
    也可以
    var map = {};
    {} 是 new Object() 的字面量3 这样写
    map[mosn] = cause
      

  2.   

    LZ自己定义一个HaspMap类吧。
    function HashMap(){
    this.map = {};
    }
    HashMap.prototype = {
    put : function(key , value){
    this.map[key] = value;
    },
    get : function(key){
    if(this.map.hasOwnProperty(key)){
    return this.map[key];
    }
    return null;
    },
    remove : function(key){
    if(this.map.hasOwnProperty(key)){
    return delete this.map[key];
    }
    return false;
    },
    removeAll : function(){
    this.map = {};
    },
    keySet : function(){
    var _keys = [];
    for(var i in this.map){
    _keys.push(i);
    }
    return _keys;
    }
    };
    HashMap.prototype.constructor = HashMap;
    var hashMap = new HashMap();
    hashMap.put('key' ,'value');
    hashMap.put('key1' ,'value');
    console.log(hashMap.get('key'));
    console.log(hashMap.keySet());
    console.log(hashMap.remove('key'));
    console.log(hashMap.keySet());
      

  3.   

    关联数组  
    var a=[];
    赋值
    a[key]=value;
    删除
    delete a[key]
    遍历用
    for(var i in a){
       alert(a[i]);
    }
    取值
    a[key]
      

  4.   

    var map = {};
    map[mosn] = cause//赋值
    console.log(map[mosn])//取值
    就ok了
      

  5.   


    function Map() {

    var struct = function(key, value) {
    this.key = key;
    this.value = value;
    }; var put = function(key, value) {
    for ( var i = 0; i < this.arr.length; i++) {
    if (this.arr[i].key === key) {
    this.arr[i].value = value;
    return;
    }
    }
    this.arr[this.arr.length] = new struct(key, value);
    };
    var get = function(key) {
    for ( var i = 0; i < this.arr.length; i++) {
    if (this.arr[i].key === key) {
    return this.arr[i].value;
    }
    }
    return null;
    }; var remove = function(key) {
    var v;
    for ( var i = 0; i < this.arr.length; i++) {
    v = this.arr.pop();
    if (v.key === key) {
    continue;
    }
    this.arr.unshift(v);
    }
    }; var size = function() {
    return this.arr.length;
    }; var isEmpty = function() {
    return this.arr.length <= 0;
    }; this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
    }