Inventory Items and prototypal inheritance in Javascript
Having a bunch of items in Polo’s House game inventory, requires a lot of typing and the majority of the items are quite similar. For example an apple, a pear, other fruits, share most of the same features.
For instance, we can define an apple item in JSON (which can be converted to a JS object with ease using JSON.parse()
) like this:
{
"type": "food",
"name": "apple",
"display": "apple",
"description": "shiny red apple",
"image": "apple.png",
"...": ".. other stuff excluded for simplicity ...",
"activity": "Eat",
"animation": {
"names": "..."
}
}
Wouldn’t be great if we can reuse the definition of the apple in other fruit? Well, this is Javascript, we can take advantage of the prototypal inheritance.