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: Dushakar Maktilar
Country: Chad
Language: English (Spanish)
Genre: Marketing
Published (Last): 9 January 2007
Pages: 423
PDF File Size: 10.36 Mb
ePub File Size: 6.97 Mb
ISBN: 662-4-11424-871-1
Downloads: 60205
Price: Free* [*Free Regsitration Required]
Uploader: Kakus

So far, we have used objects as loose aggregations javascript.programkwanie values, adding and altering their properties whenever we saw fit. Each living thing in the terrarium gets a new property, energywhich is reduced by performing actions, and increased by eating things.

JavaScript Programowanie obiektowe : Stoyan Stefanov :

It is possible to write a function that does this, obiektoowe, by using the following trick:. Firstly, having a small, clearly described interface makes an object easier to use. Constructors take a very central role, they are the things that give oboektowe object type its name, and when you need to get at a prototype, you have to go to the constructor and take its prototype property.

We already saw simple examples of this, like the toString method on objects.

JavaScript Programowanie obiektowe

By copying its kick and take methods into another prototype, we mix smallness into this prototype. A related problem is that it can often be javascript.prograjowanie to extend the prototypes of standard constructors such as Object and Array with new useful functions.

We could go over the properties of Terrarium. This usually makes things easier to model in a program, but of course has the drawback of being wildly inaccurate.

When it has enough energy, a thing can reproduce 2generating a new creature of the same kind. It returns a function like print which, instead of adding to the output, replaces its previous output.

Instead of adding arguments to a function, this one adds a this object, using the first argument to the function’s apply method:.


This way, when someone, possibly yourself three months after you wrote it, wants to work with the interface, they can quickly javascript.programowwnie how to use it, and do not have to study the whole program.

This is one of the not-so-well-designed aspects of JavaScript. Do not write out all the directions, use the each method on the directions dictionary.

Object-oriented Programming — Eloquent JavaScript

All objects that have jjavascript.programowanie meaningful toString method can be given to print and other functions that need to convert values to strings, and the correct string will be produced, no matter how their toString method chooses to build this string.

A function is called as a method when it is looked up as a property, and immediately called, as in object. Now those were games! This way, javasctipt.programowanie method function can look up the correct function value for itself.

But they all are too complex to be discussed here. In fact there are, as usual, multiple good approaches to this. To deal with this, we have to teach our lichen-eaters about long-term sustainable farming. One can use an array of arrays, like this: 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.

Modify the lichenPlan array to include a few of these, and try them out. The ‘reached list’ we used at the end of chapter 7 is an example of this: Multiple inheritance, though extremely useful in some situations, can be safely ignored most of the time.

The x coordinate of the positions on the grid can be used to determine when the end of a line is reached. Consider reading the third edition instead. The trick of only eating when it spots two pieces of food doesn’t work very well for them, because their food moves around so much it javascript.programowanke rare to find two obieitowe one place.


If there is no such property, it continues searching the prototype of the object, and then the prototype of the prototype, and so on. 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.

It will be stored in the object, and the next time we want to go over the collection of cats, calling object. We had to write an empty, useless constructor for StrangeArray in the example above.

This technique is called ‘ inheritance’. Put a for loop for the x coordinate inside a loop for the y coordinate. Having access to the prototype of an object can be very convenient, javascript.pdogramowanie making it a property like that was not a very good idea. If you need access to the whole this object, you can store that in a variable too.

JavaScript. Programowanie obiektowe

Anyway, when you are done watching this fascinating show, shut it down: This means that all simple objects have a toString method, which converts them to a string. If you have obiwktowe object hedgehogwhich has the unique habit of rolling up when it is kicked, you can just change its kick method. It returns a value that can be given to clearInterval javascript.programownaie stop its effect. It should only be called on constructors whose prototype does actually have a character property.

The first is used to cause its first argument a function, or a string containing JavaScript code to be executed periodically. Fortunately, this terrarium-simulator is not required to be accurate in any way, so we can get away with it.

Author: admin