Skip to content

Commit 1545116

Browse files
authored
Merge pull request #99 from covalt1985/master
Object methods, "this"
2 parents 4cad987 + 769f335 commit 1545116

File tree

10 files changed

+168
-167
lines changed

10 files changed

+168
-167
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
**Error**!
1+
**Błąd**!
22

3-
Try it:
3+
Sprawdź ten kod:
44

55
```js run
66
let user = {
77
name: "John",
88
go: function() { alert(this.name) }
99
}
1010

11-
(user.go)() // error!
11+
(user.go)() // błąd!
1212
```
13+
W większości przeglądarek wiadomość o błędzie nie zawiera zbyt wielu szczegółów mówiących co poszło nie tak.
1314

14-
The error message in most browsers does not give us much of a clue about what went wrong.
15+
**Błąd wystąpił ponieważ nie ma średnika po `user = {...}`.**
1516

16-
**The error appears because a semicolon is missing after `user = {...}`.**
17-
18-
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
17+
JavaScript nie wstawia automatycznie średnika przed nawiasem `(user.go)()`, więc czyta kod w ten sposób:'
1918

2019
```js no-beautify
2120
let user = { go:... }(user.go)()
2221
```
2322

24-
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
23+
Teraz widzimy, że taka składnia jest w zasadzie wywołaniem obiektu `{ go: ... }` jako funkcji z argumentem `(user.go)`. W dodatku wywołanie to znajduje się w tej samej linijce co `let user`, więc obiekt `user` nie został jeszcze nawet zdefiniowany, dlatego pojawia się błąd.
2524

26-
If we insert the semicolon, all is fine:
25+
Jeśli wstawimy średnik, kod będzie działać:
2726

2827
```js run
2928
let user = {
@@ -34,4 +33,4 @@ let user = {
3433
(user.go)() // John
3534
```
3635

37-
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
36+
Miej na uwadze, że nawiasy wokół `(user.go)` nie mają tu żadnego znaczenia. Zazwyczaj służą do zachowania kolejności wykonywania działań, jednak w tym przypadku kropka `.` i tak ma pierwszeństwo. Jedynie średnik jest tu niezbędny.

Diff for: 1-js/04-object-basics/04-object-methods/2-check-syntax/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Syntax check
5+
# Sprawdzenie składni
66

7-
What is the result of this code?
7+
Jaki będzie rezultat wykonania tego kodu ?
88

99

1010
```js no-beautify
@@ -16,4 +16,4 @@ let user = {
1616
(user.go)()
1717
```
1818

19-
P.S. There's a pitfall :)
19+
P.S. Jest tu pułapka :)
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11

2-
Here's the explanations.
2+
Oto wyjaśnienie.
33

4-
1. That's a regular object method call.
4+
1. Jest to zwykłe wywołanie metody obiektu.
55

6-
2. The same, brackets do not change the order of operations here, the dot is first anyway.
6+
2. Tak jak powyżej, nawiasy nie zmieniają tutaj kolejności wykonywania działań, kropka i tak ma pierwszeństwo.
77

8-
3. Here we have a more complex call `(expression).method()`. The call works as if it were split into two lines:
8+
3. Tutaj mamy bardziej złożone wywołanie `(expression).method()`. Wywołanie działa tutaj tak jakby było rozbite na dwie linijki kodu:
99

1010
```js no-beautify
11-
f = obj.go; // calculate the expression
12-
f(); // call what we have
11+
f = obj.go; // przypisanie jako wartość zmiennej
12+
f(); // wywołanie stworzonej zmiennej
1313
```
1414

15-
Here `f()` is executed as a function, without `this`.
15+
`f()` jest tutaj wywoływane jako funkcja, bez `this`.
1616

17-
4. The similar thing as `(3)`, to the left of the dot `.` we have an expression.
17+
4. Podobna sytuacja jak w `(3)`, po lewej stronie od kropki `.` mamy wyrażenie.
1818

19-
To explain the behavior of `(3)` and `(4)` we need to recall that property accessors (dot or square brackets) return a value of the Reference Type.
20-
21-
Any operation on it except a method call (like assignment `=` or `||`) turns it into an ordinary value, which does not carry the information allowing to set `this`.
19+
Żeby wyjaśnić zachowanie `(3)` i `(4)` musimy przypomnieć sobie, że akcesory właściwości (kropki lub nawiasy kwadratowe) zwracają wartość Typu Referencji.
2220

21+
Każda inna operacja niż wywołanie metody (jak przypisanie `=` lub `||`) zmienia Typ Referencji na zwykłą wartość, która nie zawiera informacji pozwalającej ustalić wartości `this`.

Diff for: 1-js/04-object-basics/04-object-methods/3-why-this/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 3
22

33
---
44

5-
# Explain the value of "this"
5+
# Określ wartość "this"
66

7-
In the code below we intend to call `obj.go()` method 4 times in a row.
7+
W poniższym kodzie chcemy wywołać metodę `obj.go()` cztery razy pod rząd.
88

9-
But calls `(1)` and `(2)` works differently from `(3)` and `(4)`. Why?
9+
Jednak wywołania `(1)` i `(2)` działają inaczej niż `(3)` i `(4)`. Dlaczego?
1010

1111
```js run no-beautify
1212
let obj, method;

Diff for: 1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**Answer: an error.**
1+
**Odpowiedź: błąd.**
22

3-
Try it:
3+
Uruchom ten kod:
44
```js run
55
function makeUser() {
66
return {
@@ -14,26 +14,26 @@ let user = makeUser();
1414
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
1515
```
1616

17-
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
17+
Jest to spowodowane tym, że reguły ustalające wartość `this` nie uwzględniają faktu istnienia obiektu. Znaczenie ma tylko moment wywołania.
1818

19-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
19+
Wartość `this` wewnątrz `makeUser()` jest `undefined`, ponieważ jest wywołana jako funkcja, a nie jako metoda wywołana za pomocą "kropki".
2020

21-
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
21+
Wartość `this` jest tu ustalona wyłącznie dla tej funkcji. Bloki kodu i obiekty nie są w tym przypadku brane pod uwagę.
2222

23-
So `ref: this` actually takes current `this` of the function.
23+
Zatem `ref: this` jest równoznaczne z `this` funkcji.
2424

25-
We can rewrite the function and return the same `this` with `undefined` value:
25+
Możemy napisać tę funkcję od nowa w taki sposób, że będzie zwracała takie samo `this` z wartością `undefined`:
2626

2727
```js run
2828
function makeUser(){
29-
return this; // this time there's no object literal
29+
return this; // tym razem nie jest zwracany obiekt
3030
}
3131

3232
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
3333
```
34-
As you can see the result of `alert( makeUser().name )` is the same as the result of `alert( user.ref.name )` from the previous example.
34+
Jak widzisz wynik `alert( makeUser().name )` jest taki sam jak wynik `alert( user.ref.name )` z poprzedniego przykładu.
3535

36-
Here's the opposite case:
36+
A tutaj odwrotna sytuacja:
3737

3838
```js run
3939
function makeUser() {
@@ -52,4 +52,4 @@ let user = makeUser();
5252
alert( user.ref().name ); // John
5353
```
5454

55-
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
55+
Teraz kod działa prawidłowo, ponieważ `user.ref()` jest metodą, a wartością `this` jest obiekt przed kropką `.`.

Diff for: 1-js/04-object-basics/04-object-methods/4-object-property-this/task.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Using "this" in object literal
5+
# "this" w obiektach
66

7-
Here the function `makeUser` returns an object.
7+
Poniższa funkcja `makeUser` zwraca obiekt.
88

9-
What is the result of accessing its `ref`? Why?
9+
Jaki będzie rezultat dostępu do jego `ref`? I dlaczego?
1010

1111
```js
1212
function makeUser() {
@@ -18,6 +18,6 @@ function makeUser() {
1818

1919
let user = makeUser();
2020

21-
alert( user.ref.name ); // What's the result?
21+
alert( user.ref.name ); // Jaki będzie wynik?
2222
```
2323

Diff for: 1-js/04-object-basics/04-object-methods/7-calculator/task.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Create a calculator
5+
# Stwórz kalkulator
66

7-
Create an object `calculator` with three methods:
7+
Stwórz obiekt `calculator` z trzema metodami:
88

9-
- `read()` prompts for two values and saves them as object properties.
10-
- `sum()` returns the sum of saved values.
11-
- `mul()` multiplies saved values and returns the result.
9+
- `read()` pobiera dwie wartości z okienek dialogowych `prompt` i zachowuje je jako właściwości obiektu.
10+
- `sum()` zwraca sumę zachowanych wartości.
11+
- `mul()` mnoży zachowane wartości i zwraca wynik.
1212

1313
```js
1414
let calculator = {
15-
// ... your code ...
15+
// ... twój kod ...
1616
};
1717

1818
calculator.read();

Diff for: 1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution is to return the object itself from every call.
1+
Rozwiązaniem jest zwracanie obiektu z każdej funkcji.
22

33
```js run demo
44
let ladder = {
@@ -26,7 +26,7 @@ let ladder = {
2626
ladder.up().up().down().up().down().showStep(); // 1
2727
```
2828

29-
We also can write a single call per line. For long chains it's more readable:
29+
Przy długich łańcuchach kodu, możemy każdy człon umieszczać w osobnej linijce, dla zwiększenia czytelności:
3030

3131
```js
3232
ladder

Diff for: 1-js/04-object-basics/04-object-methods/8-chain-calls/task.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Chaining
5+
# Łączenie
66

7-
There's a `ladder` object that allows to go up and down:
7+
Mamy tutaj obiekt `ladder` który pozwala wspinać się do góry i schodzić w dół:
88

99
```js
1010
let ladder = {
@@ -15,13 +15,13 @@ let ladder = {
1515
down() {
1616
this.step--;
1717
},
18-
showStep: function() { // shows the current step
18+
showStep: function() { // pokazuje aktualną wartość step
1919
alert( this.step );
2020
}
2121
};
2222
```
2323

24-
Now, if we need to make several calls in sequence, can do it like this:
24+
Jeśli chcielibyśmy wykonać sekwencję ruchów, możemy zrobić to w ten sposób:
2525

2626
```js
2727
ladder.up();
@@ -30,10 +30,10 @@ ladder.down();
3030
ladder.showStep(); // 1
3131
```
3232

33-
Modify the code of `up`, `down` and `showStep` to make the calls chainable, like this:
33+
Zmodyfkuj kod dla `up`, `down` i `showStep` żeby można było połączyć wywołania metod, w taki sposób:
3434

3535
```js
3636
ladder.up().up().down().showStep(); // 1
3737
```
3838

39-
Such approach is widely used across JavaScript libraries.
39+
Wiele bibliotek JavaScript wykorzystuje taki sposób pisania kodu.

0 commit comments

Comments
 (0)