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: Vudozuru Mazusar
Country: Guinea
Language: English (Spanish)
Genre: Health and Food
Published (Last): 13 February 2006
Pages: 404
PDF File Size: 12.34 Mb
ePub File Size: 5.47 Mb
ISBN: 813-2-36002-702-5
Downloads: 20060
Price: Free* [*Free Regsitration Required]
Uploader: Kelar

The trick of only eating when it spots two pieces of food javascript.programlwanie work very well for them, because their food moves around so much it is rare to find two in one place.

A better approach would be to store the characters and the corresponding bug-constructors in a dictionary, and look for them there:.

Like a real wall, it doesn’t do much, it just sits there and takes up space. We can make a simple, stupid bug that always just goes south, ‘towards the light’, like this: Fortunately, all we have to do is to make the elementFromCharacter function more general.

Give the lichen-eater “c” as its character pac-man. This javascgipt.programowanie does not currently work correctly in Internet Explorer 8, which apparently has some problems with overriding built-in prototype properties. When it has an energy of 30 or more, and there is room near it, it reproduces. The first is used to cause its first argument obiktowe function, or a string containing JavaScript code to be executed periodically.

JavaScript. Programowanie obiektowe

This allows us to have all kinds of bugs, without changing anything about the terrarium code. Unfortunately, it does make looping over the properties of an object a bit clumsier. JavaScript does not allow an object to have multiple prototypes, and even if it did, the problem would not be quite that easy to solve. I failed to find a really good way to prevent these creatures from either going extinct right away or gobbling up all lichen-eaters and then going extinct. This requires some discipline, but the result is a biotope that does not destroy itself.


JavaScript Programowanie obiektowe

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. Multiple inheritance, though extremely useful in some situations, can be safely ignored most of the time. Using this, we could rewrite our properties method like this:. We will take a rather simple approach, and make the terrarium a two-dimensional grid, like the second map in chapter 7.

This way, objects of the new type can be polymorphically used in all the places where objects of the old type could be used.

It should only be called on constructors whose prototype does actually have a character property. SmallItem can be seen as such a prototype.

Editions of Object-Oriented JavaScript by Stoyan Stefanov

It has four methods: Unless someone actually messes with the method in Object. By always picking a random direction, it will often move back and forth without getting anywhere. Thus, any this variable outside of the function will not be visible. This is easy to do, and in some cases it is the best solution, but in this case there is a cleaner way. 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.

Thus, the question of whether inheritance is being used correctly boils down to the question of whether the resulting code works correctly and avoids useless repetitions. 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 method has to go over all the bugs on the grid, ask them for an action, and execute the given action. 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.


Try to find a way to make it fit in the ecosystem without dying out too quickly. Because of this, JavaScript programmers have invented many different approaches to inheritance. We used only three functions, makeReachedListstoreReachedand findReached to interact with it. Prototypes are a powerful, if somewhat confusing, part of the way JavaScript objects work.

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: When the act method is called, it is given an object with information about the surroundings of the bug in javascript.prpgramowanie. For each of the eight directions, it contains a property. 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.

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.

Note that the action function is called with both the name of the property and the value it has in the object. This will make it easier to write the toString function of the terrarium later.

Each property holds a string of one character, as javasript.programowanie by characterFromElementindicating what the bug can see in that direction.

This is one of the not-so-well-designed aspects of JavaScript.