|Published (Last):||9 May 2015|
|PDF File Size:||2.28 Mb|
|ePub File Size:||10.90 Mb|
|Price:||Free* [*Free Regsitration Required]|
Books were being written, courses given, programming languages developed. All of a sudden, everybody was extolling the virtues of object-orientation, enthusiastically applying it to every problem, convincing themselves they had finally found the right way to write programs. When a process is hard and confusing, people are always on the lookout for a magic solution. When something looking like such a solution presents itself, they are prepared to become devoted followers.
What I want to do is warn the reader against developing an unhealthy attachment to them. So far, we have used objects as loose aggregations of values, adding and altering their properties whenever we saw fit. In an object-oriented approach, objects are viewed as little worlds of their own, and the outside world may touch them only through a limited and well-defined interface, a number of specific methods and properties.
For example, if there are different rabbits, the speak method must indicate which rabbit is speaking. 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. A obieotowe is called as a method when it is looked up as a property, and immediately called, as in object.
The simple objects we have used so far are based on the most basic prototype, which is associated with the Object constructor. This means that all simple objects have a toString method, which converts them to a string.
Our rabbit objects are based on the prototype associated with the Rabbit constructor. You can use a constructor’s prototype property to get access to, well, their prototype:. Because the rabbit prototype is itself an object, it is based on the Object prototype, and shares its toString method. The properties of the prototype influence the object based on it, but the properties of this object never change the prototype.
For example, it might become necessary for our rabbits to dance. Here is a new approach to the Rabbit constructor:. It means that using an object to store a set of things, such as the cats from chapter 4can go wrong. If, for example, we wondered whether there is a cat called “constructor”we would have checked it like this:.
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. We are interested only in the properties that the object itself has.
This can be solved by doing something even uglier:. This example does not currently work correctly in Internet Explorer 8, which apparently has some problems with overriding built-in prototype properties. Unless someone actually messes with the method in Object. There is one more catch, however.
Objects play both the role of ‘values with methods’, for which prototypes work great, and ‘sets of properties’, for which prototypes only get in the way.
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.
Because you can use it to look things up by name, we will call it a Dictionary.
Note that the values property of a Dictionary object is not part of this interface, it is an internal detail, and when you are using Dictionary objects you do not need to directly use it. This way, when someone, possibly yourself three months after you wrote it, wants to work with the interface, they can quickly see how to use it, and do not have to study the whole program.
Firstly, having a small, clearly described interface makes an object easier to use. 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.
When outside code is accessing every single property and detail in the object, you can not change any of them without also updating a lot of other code. If outside code only uses a small interface, you can do what you want, as long as you do not change the interface. This way, if they ever want to change their object in such a way that it no longer has a length property, for example because it now has some internal array whose length it must return, they can update the function without changing the interface.
Of course, if you are careful, and you do not expect your code to have to coexist with badly-written code, adding methods to standard prototypes is a perfectly good technique. There will be some objects involved this is, after all, the chapter on object-oriented programming. We will take a rather simple approach, and make the terrarium a two-dimensional grid, like the second map in chapter 7.
On this grid there are a number of bugs. When the terrarium is active, all the bugs get a chance to take an action, such as moving, every half second. This usually makes things easier to model in a program, but of course has the drawback of being wildly inaccurate. Fortunately, this terrarium-simulator is not required to be accurate in any way, so we can get away with it.
Then there is stepwhich allows all the bugs in the terrarium to move one step, if they so desire. And finally, there are start and stopwhich control whether the terrarium is ‘running’. When it is running, step is automatically called every half second, so the bugs keep moving. In chapter 7 we used three functions, pointaddPointsand samePoint to work with points. This time, we will use a constructor and two methods. Write the constructor Pointwhich takes two arguments, the x and y coordinates of the point, and produces an object with x and y properties.
Give the prototype of this constructor a method addwhich takes another point as argument and returns a new point whose x and y are the sum of the x and y of the two given points.
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.