I’m developing a HTML 5 application.
In Javascript I have defined a custom class and a HashTable implementation:
function Card(newId, newName, newDescription)
{
this.id = newId;
this.name = newName;
this.description = newDescription;
}
function HashTable()
{
var hashTableItems = {};
this.SetItem = function(key, value)
{
hashTableItems[key] = value;
}
this.GetItem = function(key)
{
return hashTableItems[key];
}
}
I use HashTable to add Card’s objects. I use this code to add cards:
...
var card = new Card(id, name, description);
$.viacognitaspace.cards.SetItem(id, card);
...
My problem is when I call HashTable.GetItem and I don’t know how to cast object returned to Card class.
var cardObject = $.viacognitaspace.cards.GetItem(cardNumber);
Here, cardObject is undefined.
If I do this:
$('#cardName').text(cardObject.name);
I get an error.
How can I fix this?
Try modifying your code to the following:
You also need to create an instance of the
HashTable function.Example:
http://jsfiddle.net/3BWpM/