Ximi Elga


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: Aranris Modal
Country: Greece
Language: English (Spanish)
Genre: Technology
Published (Last): 26 June 2011
Pages: 232
PDF File Size: 7.80 Mb
ePub File Size: 7.51 Mb
ISBN: 609-5-97800-578-8
Downloads: 88382
Price: Free* [*Free Regsitration Required]
Uploader: Malashura

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. Here is a new approach to the Rabbit constructor: If you rewrite the constructor, do not forget to re-register it in the creatureTypes dictionary, or the terrarium will continue to use the old constructor.

This time, we will use a constructor and two methods. Such iavascript.programowanie printer only takes one argument, javascriipt.programowanie after partially applying it there are no arguments left, and it becomes a function of zero arguments. If, for example, we wondered whether there is a cat called “constructor”we would have checked obiektlwe like this:. 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.

Object-oriented Programming — Eloquent JavaScript

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. This will not work. You might be tempted to use each on the grid, and just handle the bugs we come across. 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. When an object does too much, it becomes a big mess of functionality, and a formidable source of confusion.

This example does not currently work correctly in Internet Explorer 8, which apparently has some problems with overriding built-in prototype properties. For most actions, the action also contains extra information, such as the direction the bug wants to go. 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. For that second problem, this page conveniently provides a function called inPlacePrinter.


When a program is not ‘truly object-oriented’, whatever that means, it is considered decidedly inferior. This technique is called polymorphism, and it is arguably the most powerful aspect of object-oriented programming. It means that using an object to store a set of things, such as the cats from chapter 4can go wrong.

This file is part of the first edition of Eloquent JavaScript. This duplicates a line. Many people define, for example, inherit and method methods on objects.

We also provide a convenient shorthand for this, an extend method, which clones the object it is applied to and adds to this clone the properties in the object that it is given as an argument. In this case there is not much harm obiektowr, but there are situations where this would cause a problem. Earlier, I mentioned that the terrarium will ask the bugs what action they want to take.

The first is used to cause its first argument a function, or a string containing JavaScript code to oboektowe executed periodically. Firstly, note that it lets them move, javascript.prograjowanie doesn’t make them move. Some problems are best solved by building a complex family tree of types. Like a real wall, it doesn’t do much, it just sits there and takes up space.

JavaScript Programowanie obiektowe

We could also use the each method in directions to build the array, which is better already. Sure, duplicating the line is shorter than calling the Item. Constructors take a very central role, they are the things that give an 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.

This object will use the direction names we saw earlier “n””ne”etcetera as property names.

We had to write an empty, useless constructor for StrangeArray in the example above. This will work as follows: The x coordinate of the positions on the grid can be used to determine when the end of a line is reached.

What I want to do is warn the reader against developing an unhealthy attachment to them. The situation is, of course, that a piano has keys, so it would be better to give it a property keysand possibly another property pedalsboth holding arrays.


This usually makes things easier to model in a program, but of course has the drawback of being wildly inaccurate. 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.

This allows us to have all kinds of bugs, javasxript.programowanie changing anything about the terrarium code. A function is called as a method when it is looked up as a property, and immediately called, as in object. Try to find a way to make it fit in the ecosystem without dying out too quickly. We could, for example, replace forEach and map with methods on arrays, and make the startsWith function we wrote in chapter 4 a method on strings.

Especially the Array and String prototypes in JavaScript jaavascript.programowanie use a few more basic methods.

For example, it might become necessary for our rabbits to dance. When something looking like such a solution presents itself, they are prepared to become devoted followers. A better approach would be to store the characters and the corresponding bug-constructors in a dictionary, and look for them there: Instead of adding arguments to a function, this one adds a this object, using the first argument to javasrcipt.programowanie function’s apply method: In this case, we need to represent a grid of values, so I wrote a Grid type, which supports the operations that the terrarium will need.

First, its constructor, which takes a plan an array of strings as argument, and initialises its grid.

Do not write out all the directions, use the each method on the directions dictionary. In this chapter, we will discuss these ideas, along with JavaScript’s rather eccentric take on them.

That is exactly what we need for the onStep property. In chapter 7 we used three functions, pointaddPointsand samePoint to work with points.