M.K Chae M.K Chae - 2 months ago 7
jQuery Question

how can i resolve this matter "Uncaught TypeError: Cannot read property 'get' of undefined"

Map = function() {
this.map = new Object();
};
Map.prototype = {
put: function(key, value) {
this.map[key] = value;
},
get: function(key) {
return this.map[key];
}
}; //skip details

var _members = new Array(5);
for (var i = 0; i < 5; i++) {
var map = new Map();
map.put("id", "hello");
_members[i] = map;
}

for (var i = 0; i < _members.length; i++) {
var m = new Map();
m = _members[i];
alert(m.get("id")); //oops

}


Please help me.
I want use hashmap in arrays. but the error was encounted alert() line
whatever it was encounted using pop()
I dont know what can i do. wait for kind answer :)

Answer

Why are you trying to implement standard data types, which is already exists in Javascript.

Try use Map from es2015.

But if you insist on its implementation, it will work:

MyMap = function() {
  this.map = new Object();
};
MyMap.prototype = {
  put: function(key, value) {
    this.map[key] = value;
  },
  get: function(key) {
    return this.map[key];
  }
}; //skip details

 var _members = new Array(5);
 for (var i = 0; i < 5; i++) {
     var map = new MyMap();
     map.put("id", "hello");
     _members[i] = map;
   }

for (var i = 0; i < _members.length; i++) {
  m = _members[i];
  alert(m.get("id")); // no oops
}
Comments