Skip to content

Commit a9bebc2

Browse files
authored
Merge pull request #149 from javascript-tutorial/revert-121-1.4.4
Revert "Object methods, "this""
2 parents 9da9e2c + 4bffc81 commit a9bebc2

File tree

11 files changed

+247
-251
lines changed

11 files changed

+247
-251
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
**Odpověď: chyba.**
1+
**Answer: an error.**
22

3-
Zkuste to:
3+
Try it:
44
```js run
5-
function vytvořUživatele() {
5+
function makeUser() {
66
return {
7-
jméno: "Jan",
8-
odkaz: this
7+
name: "John",
8+
ref: this
99
};
1010
}
1111

12-
let uživatel = vytvořUživatele();
12+
let user = makeUser();
1313

14-
alert( ivatel.odkaz.jméno ); // Chyba: Nelze načíst vlastnost 'jméno' objektu undefined
14+
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
1515
```
1616

17-
Je to proto, že pravidla, která nastavují `this`, se nedívají do definice objektu. Záleží jen na okamžiku volání.
17+
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
1818

19-
Zde je hodnota `this` uvnitř `vytvořUživatele()` `undefined`, protože tato funkce je volána jako funkce, ne jako metoda „tečkovou“ syntaxí.
19+
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
2020

21-
Hodnota `this` je stejná pro celou funkci, bloky kódu a objektové literály ji neovlivňují.
21+
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
2222

23-
Takže `odkaz: this` vezme ve skutečnosti aktuální `this` této funkce.
23+
So `ref: this` actually takes current `this` of the function.
2424

25-
Můžeme funkci přepsat a vrátit stejné `this` s hodnotou `undefined`:
25+
We can rewrite the function and return the same `this` with `undefined` value:
2626

2727
```js run
28-
function vytvořUživatele(){
29-
return this; // tentokrát tady není objektový literál
28+
function makeUser(){
29+
return this; // this time there's no object literal
3030
}
3131

32-
alert( vytvořUživatele().jméno ); // Chyba: Nelze načíst vlastnost 'jméno' objektu undefined
32+
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
3333
```
34-
Jak vidíte, výsledek `alert( vytvořUživatele().jméno )` je stejný jako výsledek `alert( uživatel.odkaz.jméno )` z předchozího příkladu.
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.
3535

36-
Toto je opačný příklad:
36+
Here's the opposite case:
3737

3838
```js run
39-
function vytvořUživatele() {
39+
function makeUser() {
4040
return {
41-
jméno: "Jan",
41+
name: "John",
4242
*!*
43-
odkaz() {
43+
ref() {
4444
return this;
4545
}
4646
*/!*
4747
};
4848
}
4949

50-
let uživatel = vytvořUživatele();
50+
let user = makeUser();
5151

52-
alert( ivatel.odkaz().jméno ); // Jan
52+
alert( user.ref().name ); // John
5353
```
5454

55-
Teď to funguje, protože `uživatel.odkaz()` je metoda. A hodnota `this` se nastaví na objekt před tečkou `.`.
55+
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.

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

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

33
---
44

5-
# Použití „this“ v objektovém literálu
5+
# Using "this" in object literal
66

7-
Zde funkce `vytvořUživatele` vrátí objekt.
7+
Here the function `makeUser` returns an object.
88

9-
Jaký je výsledek přístupu k jeho vlastnosti `odkaz`? Proč?
9+
What is the result of accessing its `ref`? Why?
1010

1111
```js
12-
function vytvořUživatele() {
12+
function makeUser() {
1313
return {
14-
jméno: "Jan",
15-
odkaz: this
14+
name: "John",
15+
ref: this
1616
};
1717
}
1818

19-
let uživatel = vytvořUživatele();
19+
let user = makeUser();
2020

21-
alert( ivatel.odkaz.jméno ); // Jaký je výsledek?
21+
alert( user.ref.name ); // What's the result?
2222
```
2323

1-js/04-object-basics/04-object-methods/7-calculator/_js.view/solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
let kalkulátor = {
2-
součet() {
1+
let calculator = {
2+
sum() {
33
return this.a + this.b;
44
},
55

6-
součin() {
6+
mul() {
77
return this.a * this.b;
88
},
99

10-
načti() {
10+
read() {
1111
this.a = +prompt('a?', 0);
1212
this.b = +prompt('b?', 0);
1313
}

1-js/04-object-basics/04-object-methods/7-calculator/_js.view/test.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11

2-
describe("kalkulátor", function() {
2+
3+
describe("calculator", function() {
34

4-
context("když zadáme 2 a 3", function() {
5+
context("when 2 and 3 entered", function() {
56
beforeEach(function() {
67
sinon.stub(window, "prompt");
78

89
prompt.onCall(0).returns("2");
910
prompt.onCall(1).returns("3");
1011

11-
kalkulátor.načti();
12+
calculator.read();
1213
});
1314

1415
afterEach(function() {
1516
prompt.restore();
1617
});
1718

18-
it('funkce načti načte dvě hodnoty a uloží je jako vlastnosti objektu', function () {
19-
assert.equal(kalkulátor.a, 2);
20-
assert.equal(kalkulátor.b, 3);
19+
it('the read get two values and saves them as object properties', function () {
20+
assert.equal(calculator.a, 2);
21+
assert.equal(calculator.b, 3);
2122
});
2223

23-
it("součet je 5", function() {
24-
assert.equal(kalkulátor.součet(), 5);
24+
it("the sum is 5", function() {
25+
assert.equal(calculator.sum(), 5);
2526
});
2627

27-
it("součin je 6", function() {
28-
assert.equal(kalkulátor.součin(), 6);
28+
it("the multiplication product is 6", function() {
29+
assert.equal(calculator.mul(), 6);
2930
});
3031
});
3132

Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11

22
```js run demo solution
3-
let kalkulátor = {
4-
součet() {
3+
let calculator = {
4+
sum() {
55
return this.a + this.b;
66
},
77

8-
součin() {
8+
mul() {
99
return this.a * this.b;
1010
},
1111

12-
načti() {
12+
read() {
1313
this.a = +prompt('a?', 0);
1414
this.b = +prompt('b?', 0);
1515
}
1616
};
1717

18-
kalkulátor.načti();
19-
alert( kalkulátor.součet() );
20-
alert( kalkulátor.součin() );
18+
calculator.read();
19+
alert( calculator.sum() );
20+
alert( calculator.mul() );
2121
```

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

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

33
---
44

5-
# Vytvořte kalkulátor
5+
# Create a calculator
66

7-
Vytvořte objekt `kalkulátor` se třemi metodami:
7+
Create an object `calculator` with three methods:
88

9-
- `načti()` se zeptá na dvě hodnoty a uloží je jako vlastnosti objektu pod názvy po řadě `a` a `b`.
10-
- `součet()` vrátí součet uložených hodnot.
11-
- `součin()` vynásobí uložené hodnoty mezi sebou a vrátí výsledek.
9+
- `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively.
10+
- `sum()` returns the sum of saved values.
11+
- `mul()` multiplies saved values and returns the result.
1212

1313
```js
14-
let kalkulátor = {
15-
// ... váš kód ...
14+
let calculator = {
15+
// ... your code ...
1616
};
1717

18-
kalkulátor.načti();
19-
alert( kalkulátor.součet() );
20-
alert( kalkulátor.součin() );
18+
calculator.read();
19+
alert( calculator.sum() );
20+
alert( calculator.mul() );
2121
```
2222

2323
[demo]
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11

2-
let žebřík = {
3-
stupeň: 0,
4-
nahoru: function() {
5-
this.stupeň++;
2+
let ladder = {
3+
step: 0,
4+
up: function() {
5+
this.step++;
66
return this;
77
},
8-
dolů: function() {
9-
this.stupeň--;
8+
down: function() {
9+
this.step--;
1010
return this;
1111
},
12-
<<<<<<< HEAD
13-
zobrazStupeň: function() {
14-
alert(this.stupeň);
15-
=======
1612
showStep: function() {
1713
alert(this.step);
1814
return this;
19-
>>>>>>> 71da17e5960f1c76aad0d04d21f10bc65318d3f6
2015
}
2116
};
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11

2-
describe('Žebřík', function() {
2+
describe('Ladder', function() {
33
before(function() {
44
window.alert = sinon.stub(window, "alert");
55
});
66

77
beforeEach(function() {
8-
žebřík.stupeň = 0;
8+
ladder.step = 0;
99
});
1010

11-
it('nahoru() by mělo vrátit this', function() {
12-
assert.equal(žebřík.nahoru(), žebřík);
11+
it('up() should return this', function() {
12+
assert.equal(ladder.up(), ladder);
1313
});
1414

15-
it('dolů() by mělo vrátit this', function() {
16-
assert.equal(žebřík.dolů(), žebřík);
15+
it('down() should return this', function() {
16+
assert.equal(ladder.down(), ladder);
1717
});
1818

19-
it('zobrazStupeň() by mělo volat alert', function() {
20-
žebřík.zobrazStupeň();
19+
it('showStep() should call alert', function() {
20+
ladder.showStep();
2121
assert(alert.called);
2222
});
2323

24-
it('nahoru() by mělo zvýšit stupeň', function() {
25-
assert.equal(žebřík.nahoru().nahoru().stupeň, 2);
24+
it('up() should increase step', function() {
25+
assert.equal(ladder.up().up().step, 2);
2626
});
2727

28-
it('dolů() by mělo snížit stupeň', function() {
29-
assert.equal(žebřík.dolů().stupeň, -1);
28+
it('down() should decrease step', function() {
29+
assert.equal(ladder.down().step, -1);
3030
});
3131

32-
it('dolů().nahoru().nahoru().nahoru() ', function() {
33-
assert.equal(žebřík.dolů().nahoru().nahoru().nahoru().stupeň, 2);
32+
it('down().up().up().up() ', function() {
33+
assert.equal(ladder.down().up().up().up().step, 2);
3434
});
3535

3636
it('showStep() should return this', function() {
@@ -42,7 +42,7 @@ describe('Žebřík', function() {
4242
});
4343

4444
after(function() {
45-
žebřík.stupeň = 0;
45+
ladder.step = 0;
4646
alert.restore();
4747
});
4848
});
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
Řešením je v každém volání vrátit tento objekt samotný.
1+
The solution is to return the object itself from every call.
22

33
```js run demo
4-
let žebřík = {
5-
stupeň: 0,
6-
nahoru() {
7-
this.stupeň++;
4+
let ladder = {
5+
step: 0,
6+
up() {
7+
this.step++;
88
*!*
99
return this;
1010
*/!*
1111
},
12-
dolů() {
13-
this.stupeň--;
12+
down() {
13+
this.step--;
1414
*!*
1515
return this;
1616
*/!*
1717
},
18-
zobrazStupeň() {
19-
alert( this.stupeň );
18+
showStep() {
19+
alert( this.step );
2020
*!*
2121
return this;
2222
*/!*
2323
}
2424
};
2525

26-
žebřík.nahoru().nahoru().dolů().zobrazStupeň().dolů().zobrazStupeň(); // zobrazí 1, pak 0
26+
ladder.up().up().down().showStep().down().showStep(); // shows 1 then 0
2727
```
2828

29-
Můžeme také psát každé volání na nový řádek. U delšího zřetězení je to čitelnější:
29+
We also can write a single call per line. For long chains it's more readable:
3030

3131
```js
32-
žebřík
33-
.nahoru()
34-
.nahoru()
35-
.dolů()
36-
.zobrazStupeň() // 1
37-
.dolů()
38-
.zobrazStupeň(); // 0
32+
ladder
33+
.up()
34+
.up()
35+
.down()
36+
.showStep() // 1
37+
.down()
38+
.showStep(); // 0
3939
```

0 commit comments

Comments
 (0)