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: Dahn Vilabar
Country: Chile
Language: English (Spanish)
Genre: Relationship
Published (Last): 10 July 2016
Pages: 23
PDF File Size: 18.3 Mb
ePub File Size: 17.58 Mb
ISBN: 813-6-55711-505-5
Downloads: 93487
Price: Free* [*Free Regsitration Required]
Uploader: Moogujinn

This argument can be used to specify the object that the function must be applied to. This will make accidental uses of properties that are not part of an object’s interface easier to spot.

Sure, duplicating the line is shorter than calling the Item. This object will use the direction names we saw earlier “n””ne”etcetera as property names. The properties of the prototype influence the object based on it, but the properties of this object never change the prototype.

When a program is not ‘truly object-oriented’, whatever that means, it is considered javascript.prgramowanie inferior. After all, we could have simply written this: In chapter 7 we used three functions, pointaddPointsand samePoint to work with points.

That is not very practical. Jabascript.programowanie prevent this from happening too quickly, at leastwe add lichen to the terrarium. Every object is based on a prototype, which gives it a set of inherent properties.

For example, if there are different rabbits, the speak method must indicate which rabbit is speaking. Can obie,towe write a new creature, LichenEaterEater character ” “which survives by eating lichen-eaters? 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 is possible to implement a decent multiple-inheritance framework in JavaScript.

It is part of the prototype of a rabbit. To keep things clear and organised, it is important to keep the amount of methods and responsibilities that an object type has as small as possible.

To make this easy, we can use a higher-order function that takes an action as its argument. The simple objects we have used so far are based on the most basic prototype, which is associated with the Object constructor. Make sure you have defined the randomElement function from exercise 8. Note that the action function is called with both the name of the property and the value it has in the object. In this new approach to prototypes, types that need no constructor can leave it out.


Unfortunately, none of them is quite perfect. Fortunately, this terrarium-simulator is not required to be accurate in any way, so we can get away with it.

JavaScript. Programowanie obiektowe by Stoyan Stefanov on Apple Books

If you need access to the whole this object, you can store that in a variable too. Right now it contains three cases which are typed in directly, or ‘hard-coded’:.

First, its constructor, which takes a plan an array of strings as argument, and initialises its grid. We use a ‘fake constructor’, and apply instanceof to it. Because you can use it to look things up by name, we will call it a Dictionary. Getting a list of the property names in a dictionary sounds like a useful tool to have, so we add it to the Dictionary prototype.

We could also use the each method in directions to build the array, which is better already. It would be nicer if it would print automatically.

Each bug object has an act method which, when called, returns an ‘action’. This is one of the not-so-well-designed aspects of JavaScript. Anyway, when you are done watching this fascinating show, shut it down: The bugs themselves will also be objects, and these objects are responsible for deciding what they want to do.

JavaScript. Programowanie obiektowe

Can you design a function methodwhich allows you to bind an object to one of its methods without naming the object twice? A trick similar to the clone function can be used to get around it: It would also look better if, instead of printing a thousand terraria below each other, we could update a single printout of the terrarium. Multiple inheritance, though extremely useful in some situations, can be safely ignored most of the time.


Adding some functions to take care of common operations can make things a little smoother. This can be solved by doing something even uglier: 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. If we make the old prototype object the prototype of the new prototype object you may have to re-read that a few timesit will automatically have all its properties. The x coordinate of the positions on the grid can be used to determine when the end of a line is reached.

You give it the object on the left hand side, and a constructor on the right hand side, and it returns a boolean, true if the constructor’s prototype property is the direct or indirect prototype of the object, and false otherwise.

JavaScript Programowanie obiektowe

If it is, it dies, and we remove it. 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. Otherwise, if there is space to move, it moves into a random nearby empty square. Because of this, JavaScript programmers have invented many different approaches to inheritance. If you choose the right amount of turns for this hibernation, or have them wake up automatically when they smell lots of food, this could be a good strategy.

Modify the lichenPlan array to include a few of these, and try them out. You could try to make the lichen-eater-eaters ‘hibernate’ use the “wait” action for a whilewhen they fail to find food for a few turns.

Related Posts