|Published (Last):||14 April 2017|
|PDF File Size:||1.24 Mb|
|ePub File Size:||15.77 Mb|
|Price:||Free* [*Free Regsitration Required]|
It has to define some kind of representation, ways to access this representation, a way to initialise the grid from a ‘plan’ array, a way to write the content of the grid to a string for the toString method, and the movement of the bugs on the grid.
Instead of adding arguments to a function, this one adds a this object, using the first argument to the function’s apply method:. Also add a method isEqualTowhich takes a point and returns a boolean indicating whether the this point refers to the same coordinates as the given point.
It is part of the prototype of a rabbit. The sub-type starts with all the properties and methods of the super-type, it inherits them, and then modifies a few of these, and optionally adds new ones. This duplicates a line. To prevent this from happening too quickly, at leastwe add lichen to the terrarium.
It clones the object, and calls its construct method, if there is such a method, giving it the arguments that were passed to create.
In this case there is not much harm done, but there are situations where this would cause a problem. Say we have a prototype Monsterwhich has its own constructor, and we want to mix that with DetailedItem.
For that second problem, this page conveniently provides a function called inPlacePrinter. Here is a constructor for rabbits:. Sometimes, it is workable to do a ‘manual mix-in’.
Earlier, I mentioned that the terrarium will ask the bugs what action they want to take. Modify the lichenPlan array to include a few of these, and try them out. Some problems are best solved by building a complex family tree of types.
It obiektoew with an energy of 10and behaves in the following way: This can be solved by doing something even uglier:. For example, it might become necessary for our rabbits to dance. Unfortunately, when a program starts to make serious use of inheritance, this approach to objects quickly becomes clumsy. We could just replace the method of to the Terrarium prototype, but we have become very attached to the simulation of the bouncing and drunk bugs, and we would hate to break our old terrarium.
For non-method functions, this is irrelevant, hence the null. If you are old enough, you may at one time have played a ‘text adventure’ game, where you move through a virtual world by typing commands, and get textual descriptions of the things around you and the actions you perform.
Instead, I will show a very simple approach which suffices in most cases. If it is, it dies, and we remove it. In this new approach to prototypes, types that need no constructor can leave it out. Some actions require additional checking before they are executed, this is done with a separate if so that if a creature, for example, tries to walk through a wall, we do not generate an “Unsupported action” exception.