|Published (Last):||25 December 2012|
|PDF File Size:||17.6 Mb|
|ePub File Size:||19.40 Mb|
|Price:||Free* [*Free Regsitration Required]|
When the act method is called, it is given an object with information about the surroundings of the bug in question. Because the rabbit prototype is itself an object, it is based on the Object prototype, and shares its toString method. Note that the action function is called with both the name of the property and the value it has in the object. For example, if SmallItem would, for some reason, also define an inspect method, which inspect method should the new prototype use?
When it has an energy of 30 or more, and there is room near it, it reproduces. The terrarium merely provides the infrastructure that asks them what to do every half second, and if they decide to move, it makes sure this happens. It would be nice if we could add different kinds of bugs. This way, it starts with a valid object of the super-type, which it can then extend. For each of the eight directions, it contains a property.
You have to take care not to get too inheritance-happy, though. This time, we will use a constructor and two methods. If there is no direction property, or an invalid one, it leaves the variables target and valueAtTarget undefined. Firstly, having a small, clearly described interface makes an object easier to use.
For this purpose, there is a special variable called thiswhich is always present when a function is called, and which points at the relevant object when the function is called as a method.
If you rewrite the constructor, do not forget to re-register it in the creatureTypes dictionary, or the terrarium will continue to use the old constructor. Related Articles.
Ferr All objects that have a meaningful toString method can be given to print and other functions that need to convert values to strings, and the correct string will be produced, no matter how their toString method chooses to build this string. For this reason, some people prefer not to touch these prototypes at all. Adding a getLength method which only contains return this. It starts with an energy of 10and behaves in the following way: It should only be called on constructors whose prototype does actually have a character property.
Moogujinn This argument can be used to specify the object that the function must be applied to. Sure, duplicating the line is shorter than calling the Item. The properties of the prototype influence the object based on it, but the properties of this object never change the prototype. After all, we could have simply written this: In chapter 7 we used three functions, pointaddPointsand samePoint to work with points. That is not very practical.