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: Vogor Shajind
Country: Puerto Rico
Language: English (Spanish)
Genre: Software
Published (Last): 23 September 2010
Pages: 377
PDF File Size: 7.69 Mb
ePub File Size: 14.80 Mb
ISBN: 778-5-99602-308-6
Downloads: 42389
Price: Free* [*Free Regsitration Required]
Uploader: Macage

Note that the action function is called with both the name of the property and the value it has in the object.

Editions of Object-Oriented JavaScript by Stoyan Stefanov

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: This way, it starts with a valid object of the super-type, which it can then extend. On this grid there are a number of bugs. This technique is called ‘ inheritance’. Otherwise, if there is space to move, it moves into a random nearby empty square. It would be nice if we could add different kinds of bugs. 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.

If no property is found, the value undefined is given. The situation is, of course, that a piano has keys, so it would be better to give it a property keysand possibly another property pedalsobieitowe holding javascript.progfamowanie. The ability to change the behaviour of objects at will, which is what prototypical inheritance gives us, is very well suited for this. Right now it contains three cases which are typed in directly, or ‘hard-coded’: Inheritance is best used when the thing modelled by the sub-type can be said to be an object of the super-type.

Can you write a new creature, LichenEaterEater character ” “which survives by eating lichen-eaters? Overuse of inheritance is a great way to make a program into a big ugly mess. Most of the ideas behind it were not really new at the time, but they had finally gained enough momentum to start rolling, to become fashionable.


In this case, we need to represent a grid of values, so I wrote a Grid type, which supports javascrit.programowanie operations that the terrarium will need.

Give the lichen-eater “c” as mavascript.programowanie character pac-man. When the point is at the edge of the grid, use ” ” for the directions that go outside of the grid, so the bug will not try to move there.

To look up javascript.programoeanie direction these names refer to, the following dictionary object is useful: 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 javascript.progrmaowanie called as a method.

For example, a piano also has pedals. This can be solved by doing something even uglier:. When using new on this constructor, it will create a new object based on the given object. 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:.

Every object is based on a prototype, which gives it a set of inherent properties. Fortunately, such a broad range of approaches allows a programmer javasxript.programowanie choose the most suitable one for the problem he is solving, and allows certain tricks that would be utterly impossible in other languages. To make this easier, we mark both the wall and the prototype of the StupidBug with a property characterwhich holds the character that represents them.

Javascript.programowajie there is a property that has the name we are obiektiwe for, that is the value we get. 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. This requires some discipline, but the result is a biotope that does not destroy itself.

But to see what is going on, we have to repeatedly do print terrariumor we won’t see what is going on.


For javasdript.programowanie, if there are different rabbits, the speak method must indicate which rabbit is speaking. 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.

By always picking a random direction, it will often move back and forth without getting anywhere. This way, if we decide for some reason to use a different method for storing the values, we only have to rewrite valueAt and setValueAtand the other methods can stay untouched.

JavaScript. Programowanie obiektowe

The name self or that is often used for such a variable. Unless you are very lucky, the lichen-eaters will probably still go extinct after a while, because, in a time of mass starvation, they crawl aimlessly back and forth through empty space, instead of finding the lichen that is sitting just around javascriot.programowanie corner.

But then, when a bug moves South or East, obiketowe will come across it again in the same turn, and allow it to move again. After all, we could have simply written this: Because a piano has a whole array of keys, one might be tempted to make Piano a sub-type of Array obiektoae, but a piano is no array, and implementing it like that is bound to lead to all kinds of silliness.

We used only three functions, makeReachedListstoreReachedand findReached to interact with it. When a process is hard and confusing, people are always on the lookout for a magic solution.

JavaScript Programowanie obiektowe

Every object has a method called hasOwnPropertywhich tells us whether the object has a property with a given name. You have to take care not to get too inheritance-happy, though. This means that all simple objects have a toString method, which converts them javascdipt.programowanie a string.