|Published (Last):||14 February 2011|
|PDF File Size:||11.52 Mb|
|ePub File Size:||16.83 Mb|
|Price:||Free* [*Free Regsitration Required]|
Because you can use it to look things up by name, we will call it a Dictionary. Each property holds a string of one character, as returned by characterFromElementindicating what the bug can see in that direction.
You might be tempted to use each on the grid, and just handle the bugs we come across. Using this, we could rewrite our properties method like this: It should only be called on constructors whose prototype does actually have a character property.
You only have to keep the interface in mind, and do not have to worry about the rest unless you are changing the object itself. An action is an object with a type property, which names the type of action the bug wants to take, for example “move”.
Consider reading the third edition javazcript.programowanie. This object keeps track of the shape and content of the terrarium, and lets the bugs inside move. Add a method toString to the Terrarium prototype, which takes no arguments and returns a string that, when given to printshows a nice two-dimensional view of the terrarium.
This technique is called ‘ inheritance’. For this, we will use two functions provided by the browser, called setInterval and clearInterval. If outside code only uses a small interface, you can do what you want, as long as you do not change the interface.
Ah, tragedy of nature. It is important to bear in mind that inheritance, in the end, is just a trick that allows lazy 3 programmers to write less code. But then, when a bug moves South or East, we will come across it again in the same turn, and allow it to move again.
obektowe But to make the result readable, it would be nice to have a newline at the end of every row. We could go over the properties of Terrarium.
You have to take care not to get too inheritance-happy, though. Personally, I treat writing documentation as a ‘finishing touch’ to add to a system.
It starts with an energy of 10and behaves in the following way: We could also use the each method in directions to build the array, which is better already.
If you have an object hedgehogwhich has the unique habit of rolling up when it is kicked, you can just change its kick method.
The sub-type starts with all the properties and methods of the super-type, it inherits them, and then javvascript.programowanie a few of these, and optionally adds new ones. When a program is not ‘truly object-oriented’, whatever that means, it is considered decidedly inferior.
If you need access to the whole this object, you can store that in a variable too. It is possible to write a function that does this, though, by using the following trick:.
This is easy to do, and in some cases it is the best solution, but in this case there is a cleaner way.
An expression such as this one can be used to reliably work around this:.
SmallItem can be oblektowe as such a prototype. Add the method each to the prototype of Gridwhich takes a function of two arguments as its argument. Here is a new approach to the Rabbit constructor:. Can you write a new creature, LichenEaterEater character ” “which survives by eating lichen-eaters? We could put it into a function, but an even better approach is to write a constructor and a prototype specifically for situations like this, where we want to approach an object as just a set of properties.
Now that the Object prototype has a property called propertieslooping over the properties of any object, using for and inwill also give us that shared property, which is generally not what we want.