Template ‐ Introduction - ThePix/QuestJS GitHub Wiki

A template is a set of default attributes that make it easy to set your object to be a certain type of thing. If your object is an item of clothing, give it the WEARABLE template, and you have already done most of the work.

When creating an object, you pass to the createItem or createRoom the name and any number of dictionaries (a set of name-value pairs). This would typically be each template, followed by a unique dictionary for the specific object.

This example has four dictionaries, three of which are from templates:

createItem("jewellery_box", TAKEABLE(), CONTAINER(true), LOCKED_WITH("jewellery_box_key"), {
  loc:"glass_cabinet",
  examine:"A nice box."
});

Each template is actually a function that returns a dictionary, which allows them to take parameters, as is seen with CONTAINER and LOCKED_WITH above, and allows you to extend them in your custom templates.

Implementation Note 1: Template Names in Capitals

The reason for this is really just to make them stand out in your code. You can look quickly at an object, and see that one is SWITCHABLE, that one is a CONTAINER, etc., before getting to the details of what it is.

Implementation Note 2: Templates not Classes

Quest 5 is object-orientated, and it uses class inheritance to handle the general properties of an object. Thus, an apple is an instance of an edible item, which inherits from takeable items, which inherits from generic items, which inherits from generic objects (actually, it may not, but it could and arguably should). JavaScript is an object-orientated language (albeit a bit quirky) that fully supports inheritances, so why use templates instead?

Firstly, JavaScript does not support multiple inheritance (in common with many languages, such as C#, Java and Ruby). If you have a flashlight, you will want it to inherit takeable, but also to inherit switchable.

Secondly, the use of templates allows us to send parameters. We can give the flashlight a SWITCHABLE template, and at the same time set it to be turned on. Okay, we could have a class that inherits from switchable, but is turned on, but some templates are rather more complicated. You could add these parameters as additional attributes for your object, but the advantage of sending them to the template as parameters is that it is immediately clear what they relate to. It is less typing too!

There are actually a load of web pages about why inheritance in general is (often) bad, but I must admit I find the arguments unconvincing, so will not be linking to them here.