I create Simple HashMap
like this , but When I put an object
into this map as a value
, I can't call object's function
when I get it from map , I find that the object
was convert to string
by toString
function. So what should I do to put the object itself
rather than a string into map?
var g_itemMap =
{
put : function(key,value){this[key] = value},
get : function(key){return this[key]},
contains : function(key){return this.get(key) == null?false:true},
remove : function(key){delete this[key]}
}
I put the object like this:
g_itemMap.put(1, object);
And get it:
var object = g_itemMap.get(1);
When I call it's function , it went wrong:
object.somefunction();
alert can display object:
[object BitmapItem]
This code looks like working for me. You can try this;
var g_itemMap =
{
put : function(key,value){this[key] = value},
get : function(key){return this[key]},
contains : function(key){return this.get(key) == null?false:true},
remove : function(key){delete this[key]}
}
var object =
{
objectfunction: function(){
console.log('objectfunction called')
}
}
g_itemMap.put(1, object);
var o = g_itemMap.get(1);
o.objectfunction();
Fiddle link: http://jsfiddle.net/hCH8k/
var HashMap = new Object();
HashMap[Key1] = Obj1;
HashMap[Key2] = Obj2;
function get(k)
{
console.log(HashMap[k]);
}
or simply you can use
var HashMap = {"Key1":"value1","Key2":"value2"}
function get(k)
{
console.log(HashMap[k]);
}
If I try
var object = { a: function() { alert('b'); } };
var g_itemMap =
{
put : function(key,value){this[key] = value},
get : function(key){return this[key]},
contains : function(key){return this.get(key) == null?false:true},
remove : function(key){delete this[key]}
}
g_itemMap.put(1, object);
var object2 = g_itemMap.get(1);
object2.a();
does alert('b'), which looks correct, to me... :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.