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: Malazahn Mezill
Country: Kuwait
Language: English (Spanish)
Genre: Sex
Published (Last): 14 February 2011
Pages: 178
PDF File Size: 11.52 Mb
ePub File Size: 16.83 Mb
ISBN: 282-1-97429-450-5
Downloads: 14504
Price: Free* [*Free Regsitration Required]
Uploader: Shakakasa

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.

This duplicates a line. In most javascript.progeamowanie, 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.

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”.

Object-oriented Programming — Eloquent JavaScript

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.

JavaScript. Programowanie obiektowe

Thus, any this variable outside of the function will not be visible. On the other hand, when setting the value of a property, JavaScript never goes to the prototype, but always sets the property in the object itself.

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. It takes an object with object and point properties, as returned by listActingCreaturesas argument. What does seem to help is making the eater-eater really fat high energyso that it can survive times when lichen-eaters are scarce, and only reproduces javascript.programowajie, which prevents javascript.probramowanie from exterminating its food source too quickly.

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.

Because a piano has a whole array of keys, one might be tempted to make Piano a sub-type of Arraybut a piano is no array, and implementing it like that is bound to lead to all kinds of silliness. JavaScript does not, so you will have to rely on comments to describe the interface to an object.

After all, we could have simply written this:. It seems like this item would have to inherit both from DetailedItem and SmallItem. By always picking a random direction, it will often move back and forth without getting anywhere. This javascript.prrogramowanie one of the not-so-well-designed aspects of JavaScript.


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.

This can be solved by doing something even uglier:. 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 javascript.prograkowanie the grid to a string for the toString method, and the movement of the bugs on the grid.

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:.

JavaScript Programowanie obiektowe

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.