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: Akijar Mikabei
Country: Finland
Language: English (Spanish)
Genre: History
Published (Last): 9 May 2015
Pages: 71
PDF File Size: 2.28 Mb
ePub File Size: 10.90 Mb
ISBN: 150-9-17112-270-2
Downloads: 66693
Price: Free* [*Free Regsitration Required]
Uploader: Kagakree

Javascript.programiwanie 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.

Books were being written, courses given, programming languages developed. All of a sudden, everybody was extolling the virtues of object-orientation, enthusiastically applying it to every problem, convincing themselves they had finally found the right way to write programs. When a process is hard and confusing, people are always on the lookout for a magic solution. When something looking like such a solution presents itself, they are prepared to become devoted followers.

For many programmers, even today, object-orientation or their view of it is the gospel. When a program is not ‘truly object-oriented’, whatever that means, it is considered decidedly inferior. Object-orientation’s longevity can largely be explained by the fact that the ideas at its core are very solid and useful. In this chapter, we will discuss these ideas, along with JavaScript’s rather eccentric take on them. The above paragraphs are by no means meant to discredit these ideas.

What I want to do is warn the reader against developing an unhealthy attachment to them. So far, we have used objects as loose aggregations of values, adding and altering their properties whenever we saw fit. In an object-oriented approach, objects are viewed as little worlds of their own, and the outside world may touch them only through a limited and well-defined interface, a number of specific methods and properties.

The ‘reached list’ we used at the end of chapter 7 is an example of this: We used only three functions, makeReachedListstoreReachedand findReached to interact with it. These three functions form an interface for such objects. Instead of providing regular functions for working with the objects, they provide a way to create such javascriptt.programowanie, using the new keyword, and a number of methods and properties that provide the rest of the interface.

For example, if there are different rabbits, the speak method must indicate which rabbit is speaking. 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 is called as a method. A obieotowe is called as a method when it is looked up as a property, and immediately called, as in object.

Justyna Walkowska (Translator of JavaScript. Programowanie obiektowe)

This argument can be used to specify the object that the function must be applied to. For non-method functions, this is irrelevant, hence the null. When a function is called with the word new in front of it, its this variable will point at a new object, which it will automatically return unless it explicitly returns something javascript.programowanif. Functions used to create new objects like this are called constructors.

Here is a constructor for rabbits:. This makes javascript.progarmowanie easy to distinguish them from other functions. After all, we could have simply written this:. For one thing, our killerRabbit has a property called constructorwhich points at the Rabbit function that created it.

It is part of the prototype of a rabbit. Prototypes are a powerful, if somewhat confusing, part of the way JavaScript objects work. Every object is based javascri;t.programowanie a prototype, which gives it a set of inherent properties.

The simple objects we have used so far are based on the most basic prototype, which is associated with the Object constructor. This means that all simple objects have a toString method, which converts them to a string.

Our rabbit objects are based on the prototype associated with the Rabbit constructor. You can use a constructor’s prototype property to get access to, well, their prototype:. Because the rabbit prototype is itself an object, it is based on the Object prototype, and shares its toString method. The properties of the prototype influence the object based on it, but the properties of this object never change the prototype.


When looking up the value of a property, JavaScript first looks at the properties that the object itself has. If there is a property that javascript.programowanle the name we are looking for, that is javascript.proramowanie value we get. If there is no such property, it continues searching the prototype of the object, and then the prototype of the prototype, and so on. If no property is found, the value undefined is given. On the other hand, when setting the value of a property, JavaScript never goes to the prototype, but always sets the property in the object itself.

For example, it might become necessary for our rabbits to dance. Here is a new approach to the Rabbit constructor:. It means that using an object to store a set of things, such as the cats from chapter 4can go wrong. If, for example, we wondered whether there is a cat called “constructor”we would have checked it like this:.

A related problem is that it can often be practical to extend the prototypes of standard javascript.programowanue such as Object and Array with new useful functions. 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:.

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. We are interested only in the properties that the object itself has.

JavaScript. Programowanie obiektowe

Unfortunately, it does make looping over the properties of an object javascript.programowamie bit clumsier. Every object has a method called havascript.programowaniewhich tells us whether the object has a property with a given name. Using this, we could rewrite our properties method like this:. Note that the action function is called with both the name of the property and the value it has javascript.programowani the object. It will be stored in the object, and the next time we want to go over the collection of cats, calling object.

This can be solved by doing something even uglier:. This example does not currently work correctly in Internet Explorer 8, which apparently has some problems with overriding built-in prototype properties. Unless someone actually messes with the method in Object. There is one more catch, however.

Having access to the prototype of an object can be very convenient, but making it a property like that was not a very good idea. Still, Firefox is a widely used browser, so when you write a program for the web you have to be careful with this. Javaxcript.programowanie expression such as this one can be used to reliably work around this:. This is one of the jafascript.programowanie aspects of JavaScript.

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.

JavaScript Programowanie obiektowe

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.

Because you can use it to look things up by name, we will call it a Dictionary.

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 way, when someone, possibly yourself three months after you wrote it, wants to work with the interface, they can quickly see how to use it, and do not have to study the whole program.


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. Personally, I treat writing documentation as a ‘finishing touch’ to add to a system. When it feels ready, it is time to write something about it, and to see if it sounds as good in English or whatever language as it does in JavaScript or whatever programming language.

Firstly, having a small, clearly described interface makes an object easier to use. 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.

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. If outside code only uses a small interface, you can do what you want, as long as you do not change the interface. This way, if they ever want to change their object in such a way that it no longer has a length property, for example because it now has some internal array whose length it must return, they can update the function without changing the interface.

Adding a getLength method which only contains return this. Especially the Array and String prototypes in JavaScript could use a few more basic methods. 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. For this reason, some people prefer not to touch these prototypes at all.

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. There will be some objects involved this is, after all, the chapter on object-oriented programming. We will take a rather simple approach, and make the terrarium a two-dimensional grid, like the second map in chapter 7.

On this grid there are a number of bugs. When the terrarium is active, all the bugs get a chance to take an action, such as moving, every half second. This usually makes things easier to model in a program, but of course has the drawback of being wildly inaccurate. Fortunately, this terrarium-simulator is not required to be accurate in any way, so we can get away with it.

We could have used a single string, but because JavaScript strings must stay on a single line it would have been a lot harder to type. This object keeps track of the shape and content of the terrarium, and lets the bugs inside move. It has four methods: Firstly toStringwhich converts the terrarium back to a string similar to the plan it was based on, so that you can see what is going on inside it.

Then there is stepwhich allows all the bugs in the terrarium to move one step, if they so desire. And finally, there are start and stopwhich control whether the terrarium is ‘running’. When it is running, step is automatically called every half second, so the bugs keep moving. In chapter 7 we used three functions, pointaddPointsand samePoint to work with points. This time, we will use a constructor and two methods. Write the constructor Pointwhich takes two arguments, the x and y coordinates of the point, and produces an object with x and y properties.

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.

Also add a method isEqualTowhich takes a point and returns a boolean indicating whether the this point refers to the same coordinates as the given point.