|Published (Last):||1 December 2014|
|PDF File Size:||7.48 Mb|
|ePub File Size:||14.5 Mb|
|Price:||Free* [*Free Regsitration Required]|
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.
For many programmers, even today, object-orientation or their view of it is the gospel. When a program is not ‘truly object-oriented’, whatever that means, it is considered decidedly inferior. Object-orientation’s longevity can largely be explained by the fact that the ideas at its core are very solid and useful.
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.
The ‘reached list’ we used at the end of chapter 7 is an example of this: We used only three functions, makeReachedListstoreReachedand findReached to interact with it. These three functions form an interface for such objects. Instead of providing regular functions for working with the objects, they provide a way to create such objects, using the new keyword, and a number of methods and properties that provide the rest of the interface.
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 function is called as a method when it is looked up as a property, and immediately called, as in object. This argument can be used to specify the object that the function must be applied to.
For non-method functions, this is irrelevant, hence the null. When a function is called with the word new in front of it, its this variable will point at a new object, which it will automatically return unless it explicitly returns something else. Functions used to create new objects like this are called constructors. Here is a constructor for rabbits:.
Every object is based on a prototype, which gives it a set of inherent properties. 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.
If there is a property that has the name we are looking for, that is the value we get. If there is no such property, it continues searching the prototype of the object, and then the prototype of the prototype, and so on. If no property is found, the value undefined is given.
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 javascropt.programowanie it like this:.
A related problem is that it can often be practical to extend the prototypes of standard constructors such as Object and Array with new useful functions. For example, we could give all objects a method called propertieswhich returns an array with the names of the non-hidden properties that the object has:. 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. Unfortunately, it does make looping over the properties of an object a bit clumsier. Every object has a method called hasOwnPropertywhich tells us whether the object has a property with a given name.
Using this, we could rewrite our properties method like this:.
Note that the action function is called with both the name of the property and the value it has in the object. It will be stored in the object, and the next time we want to go over the collection of cats, calling object. 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.
Having access to the prototype of an object can be very convenient, but making it a property like that was not a very good idea. Still, Firefox is a widely used browser, so jwvascript.programowanie you write a program for the web you have to be careful with this.
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 jafascript.programowanie study the whole program. To prevent wasting your time, it is advisable to document your interfaces only after they have been used in a few real situations and proven themselves to be practical.
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.
For this reason, some people prefer not to touch these prototypes at all. 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.