/* Game */
function Game() {
return {
items: []
}
}
/* Item */
function Item(id, name, description, image, price) {
return {
getId: function () {
return id;
}
}
}
Item.prototype.toString = function() {
return id + ":" + name;
};
/* Logic.. */
var game = new Game();
var sword = new Item(1, "Sword", "An iron sword", "sword.png", 10);
game.items.push(sword);
console.log(game.items[0] instanceof Item); // false
console.log(typeof game.items[0]); // object
console.log(game.items[0]); // Object{}
I have a feeling that because my custom objects go into an array that they lose their 'custom type', in this case Item, when I pull them out.
How can I keep a list of Item and have Javascript find out its real type?