JS programowanie obiektowe. 1. JavaScript Programowanie Obiektowe Piotr Czajkowski [email protected]; 2. constructor new. Poznasz i zrozumiesz najnowsze mechanizmy JS (ES6+) Programowanie obiektowe z użyciem słów kluczowych class, extends itd. In the early nineties, a thing called object-oriented programming stirred up the software industry. Most of the ideas behind it were not really new at the time, but.

Author: Voodooshicage Faur
Country: Slovenia
Language: English (Spanish)
Genre: Spiritual
Published (Last): 28 April 2014
Pages: 335
PDF File Size: 11.4 Mb
ePub File Size: 2.79 Mb
ISBN: 771-8-65333-387-3
Downloads: 88203
Price: Free* [*Free Regsitration Required]
Uploader: Samucage

The situation is, of course, that a piano has keys, so it would be better to give it a property keysand possibly another property pedalsboth holding arrays. This can be solved by doing something even uglier:.

But to see what is going on, we have to repeatedly do print terrariumor we won’t see what is going on. Anyway, when you are done watching this fascinating show, shut it down: It calls this function for every point on the grid, giving it the point object for that point as its first argument, and the value that is on the grid at that point as second argument. We have based a new object type on an old one, which saved us the work of re-writing all the methods which are the same in Terrarium and LifeLikeTerrarium.

The bugs themselves will also be objects, and these objects are responsible for deciding what they want to do.

If there is a property that has the name we are looking for, that is the value we get. Still, Firefox is a widely used browser, so when you write a program for the web you have to be careful with this. When using new on this constructor, it will create a new object based on the given object. This method gathers bugs, or other things that have an act method, and stores them in objects that also contain their current position:.

You have to take care not to get too inheritance-happy, though. All of a sudden, everybody was extolling the javascript.programkwanie of object-orientation, enthusiastically applying it to every problem, convincing themselves they had finally found the right way to write programs. For example, it might become necessary for our rabbits to dance.

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. In this approach, a type is represented by its prototype, and we will use capitalised variables to store these prototypes.


Some languages chicken out and forbid it altogether, others define complicated schemes for making it work in a well-defined and practical way. Note that the action function is called with both the name of the property and the value it has in the object. When it has an energy of 30 or more, and there is room near it, it reproduces. Can you design a function methodwhich allows you to bind an object to one of its methods without naming the object twice?

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.

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. In most cases, this means the new type will still support the interface of the old type, though it might also support a few methods that the old type does not have. It would also look better if, instead of printing a thousand terraria below each other, we could update a single printout of the terrarium.

It has four methods: This way, it starts with a valid object of the super-type, which it can then extend.

JavaScript. Programowanie obiektowe

And finally, there are start and stopwhich control whether the terrarium is ‘running’. Make sure you have defined the randomElement function from exercise 8.

We could of course just type [“n”, “ne”, It returns a value that can be given to clearInterval to stop its effect. Objects are turned to strings by calling their toString method, so giving your own object types a meaningful toString is a good way to make them readable when printed out. Unless you are very lucky, the lichen-eaters will probably still go extinct obiektow a while, because, in a time javascfipt.programowanie mass starvation, they crawl aimlessly back and forth through empty space, instead of finding the lichen that is sitting just around the corner.

This usually makes things easier to model in a program, but of course obiekttowe the drawback of being wildly inaccurate.

JavaScript Programowanie obiektowe : Stoyan Stefanov :

javawcript.programowanie For non-method functions, this is irrelevant, hence the null. Modify the lichenPlan array to include a few of these, and try them out.


Can you write a new creature, LichenEaterEater character ” “which survives by eating lichen-eaters? Students of object-oriented programming can often be heard having lengthy, subtle discussions about correct and incorrect uses of inheritance. On this grid there are a number of bugs.

When looking up the value of a property, JavaScript first looks at the properties that the object itself has. The new type inherits the properties of the old type. When a program is not ‘truly object-oriented’, whatever that means, it is considered decidedly inferior.

Object-oriented Programming — Eloquent JavaScript

We used only three functions, makeReachedListstoreReachedand findReached to interact with it. Personally, I treat writing documentation as a ‘finishing touch’ to add to a system.

If there is no direction property, or an invalid one, it leaves the variables target and valueAtTarget undefined. A function is called as a method when it is looked up as a property, and immediately called, as in object.

When the act method is called, it is given an object with information about the surroundings of the bug in question. 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.

If you rewrite the constructor, do not forget to re-register it in the creatureTypes dictionary, or the terrarium will continue to use the javascript.programowani constructor.

It would be nice if we could add different kinds of bugs. To look up the direction these names refer to, the following dictionary object is useful: Sure, duplicating the line is shorter than calling the Item.

Obiekotwe could, for example, replace forEach and map with methods on arrays, and make the startsWith function we wrote in chapter 4 a method on strings.

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.