You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/04-object-methods/article.md
+30-29Lines changed: 30 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# Object methods, "this"
1
+
# Metody obiektów, "this"
2
2
3
-
Objects are usually created to represent entities of the real world, like users, orders and so on:
3
+
Obiekty zazwyczaj są tworzone po to, żeby przedstawiać rzeczywiste podmioty, takie jak użytkownicy, zadania do wykonania i tym podobne:
4
4
5
5
```js
6
6
let user = {
@@ -9,13 +9,13 @@ let user = {
9
9
};
10
10
```
11
11
12
-
And, in the real world, a user can *act*: select something from the shopping cart, login, logout etc.
12
+
I tak jak w rzeczywistości, użytkownik może *działać*: wybrać coś z koszyka, zalogować się, wylogować itd.
13
13
14
-
Actions are represented in JavaScript by functions in properties.
14
+
Czynności są w JavaScript'cie funkcjami we właściwościach obiektu.
15
15
16
-
## Method examples
16
+
## Przykłady metod
17
17
18
-
For a start, let's teach the `user`to say hello:
18
+
Na początek, nauczmy użytkownika `user`jak się przywitać:
19
19
20
20
```js run
21
21
let user = {
@@ -25,74 +25,75 @@ let user = {
25
25
26
26
*!*
27
27
user.sayHi=function() {
28
-
alert("Hello!");
28
+
alert("Cześć!");
29
29
};
30
30
*/!*
31
31
32
-
user.sayHi(); //Hello!
32
+
user.sayHi(); //Cześć!
33
33
```
34
34
35
-
Here we've just used a Function Expression to create the function and assign it to the property `user.sayHi`of the object.
35
+
Właśnie użyliśmy Wyrażenia Funkcji do stworzenia funkcji i przypisaliśmy ją do właściwości `user.sayHi`obiektu.
36
36
37
-
Then we can call it. The user can now speak!
37
+
Następnie ją wywołaliśmy. Użytkownik potrafi teraz mówić!
38
38
39
-
A function that is the property of an object is called its *method*.
39
+
Funkcja która jest właściwością obiektu nazywamy *metodą*.
40
40
41
-
So, here we've got a method `sayHi`of the object`user`.
41
+
Także mamy tutaj metodę `sayHi`obiektu`user`.
42
42
43
-
Of course, we could use a pre-declared function as a method, like this:
43
+
Oczywiście moglibyśmy również posłużyć się wcześniej zadeklarowaną funkcją jako metodą:
44
44
45
45
```js run
46
46
let user = {
47
47
// ...
48
48
};
49
49
50
50
*!*
51
-
//first, declare
51
+
//najpierw deklarujemy
52
52
functionsayHi() {
53
-
alert("Hello!");
53
+
alert("Cześć!");
54
54
};
55
55
56
-
//then add as a method
56
+
//następnie dodajemy jako metodę
57
57
user.sayHi= sayHi;
58
58
*/!*
59
59
60
-
user.sayHi(); //Hello!
60
+
user.sayHi(); //Cześć!
61
61
```
62
62
63
63
```smart header="Object-oriented programming"
64
-
When we write our code using objects to represent entities, that's called [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming), in short: "OOP".
64
+
Kiedy piszemy kod wykorzystujący obiekty do reprezentowania podmiotów, nazywamy to[programowaniem obiektowym](https://pl.wikipedia.org/wiki/Programowanie_obiektowe), w skrócie:
65
+
"OOP".
65
66
66
-
OOP is a big thing, an interesting science of its own. How to choose the right entities? How to organize the interaction between them? That's architecture, and there are great books on that topic, like "Design Patterns: Elements of Reusable Object-Oriented Software" by E.Gamma, R.Helm, R.Johnson, J.Vissides or "Object-Oriented Analysis and Design with Applications" by G.Booch, and more.
67
+
OOP to bardzo rozległy i interesujący temat. Jak wybrać właściwe podmioty? Jak stworzyć zależności między nimi? Jest to cała architektura i istnieje wiele świetnych książek traktujących ten temat, jak np. "Wzorce projektowe. Elementy oprogramowania" autorstwa E.Gamma, R.Helm, R.Johnson, J.Vissides, lub "Object-Oriented Analysis and Design with Applications" G.Booch, i wiele innych
67
68
```
68
-
### Method shorthand
69
+
### Skróty dla metod
69
70
70
-
There exists a shorter syntax for methods in an object literal:
71
+
Istnieje skrócona składnia dla metod w literałach obiektowych:
71
72
72
73
```js
73
-
//these objects do the same
74
+
//te obiekty działają tak samo
74
75
75
76
user = {
76
77
sayHi:function() {
77
-
alert("Hello");
78
+
alert("Cześć");
78
79
}
79
80
};
80
81
81
-
//method shorthand looks better, right?
82
+
//skrócona składnia wygląda lepiej, prawda ?
82
83
user = {
83
84
*!*
84
-
sayHi() { //same as "sayHi: function()"
85
+
sayHi() { //to samo co "sayHi: function()"
85
86
*/!*
86
-
alert("Hello");
87
+
alert("Cześć");
87
88
}
88
89
};
89
90
```
90
91
91
-
As demonstrated, we can omit `"function"`and just write`sayHi()`.
92
+
Jak wyżej, możemy pominąć `"function"`i po prostu użyć`sayHi()`.
92
93
93
-
To tell the truth, the notations are not fully identical. There are subtle differences related to object inheritance (to be covered later), but for now they do not matter. In almost all cases the shorter syntax is preferred.
94
+
Szczerze mowiąc, oba zapisy nie są całkowicie identyczne. Istnieją subtelne różnice między nimi, związane z dziedziczeniem (ten temat poruszymy później), ale na tem moment nie ma to znaczenia. W prawie każdym przypadku lepiej użyć skróconej wersji.
94
95
95
-
## "this" in methods
96
+
## "this" w metodach
96
97
97
98
It's common that an object method needs to access the information stored in the object to do its job.
0 commit comments