Skip to content

Commit c697dca

Browse files
authored
Merge pull request #183 from otmon76/1.9.3
Static properties and methods
2 parents 66b50be + f59c39a commit c697dca

File tree

5 files changed

+168
-168
lines changed

5 files changed

+168
-168
lines changed
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
1-
First, let's see why the latter code doesn't work.
1+
Nejprve se podívejme, proč poslední uvedený kód nefunguje.
22

3-
The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined".
3+
Důvod bude zřejmý, když se ho pokusíme spustit. Konstruktor zděděné třídy musí volat `super()`, jinak nebude „definováno“ `this`.
44

5-
So here's the fix:
5+
Zde je tedy oprava:
66

77
```js run
8-
class Rabbit extends Object {
9-
constructor(name) {
8+
class Králík extends Object {
9+
constructor(jméno) {
1010
*!*
11-
super(); // need to call the parent constructor when inheriting
11+
super(); // při dědění je nutné volat rodičovský konstruktor
1212
*/!*
13-
this.name = name;
13+
this.jméno = jméno;
1414
}
1515
}
1616

17-
let rabbit = new Rabbit("Rab");
17+
let králík = new Králík("Bobek");
1818

19-
alert( rabbit.hasOwnProperty('name') ); // true
19+
alert( králík.hasOwnProperty('jméno') ); // true
2020
```
2121

22-
But that's not all yet.
22+
To však ještě není všechno.
2323

24-
Even after the fix, there's still an important difference between `"class Rabbit extends Object"` and `class Rabbit`.
24+
I po této opravě bude stále existovat důležitý rozdíl mezi `class Králík extends Object` a `class Králík`.
2525

26-
As we know, the "extends" syntax sets up two prototypes:
26+
Jak víme, syntaxe „extends“ nastavuje dva prototypy:
2727

28-
1. Between `"prototype"` of the constructor functions (for methods).
29-
2. Between the constructor functions themselves (for static methods).
28+
1. Mezi `"prototype"` konstruktorů (pro metody).
29+
2. Mezi samotnými konstruktory (pro statické metody).
3030

31-
In the case of `class Rabbit extends Object` it means:
31+
V případě `class Králík extends Object` to znamená:
3232

3333
```js run
34-
class Rabbit extends Object {}
34+
class Králík extends Object {}
3535

36-
alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
37-
alert( Rabbit.__proto__ === Object ); // (2) true
36+
alert( Králík.prototype.__proto__ === Object.prototype ); // (1) true
37+
alert( Králík.__proto__ === Object ); // (2) true
3838
```
3939

40-
So `Rabbit` now provides access to the static methods of `Object` via `Rabbit`, like this:
40+
`Králík` tedy nyní poskytuje přístup ke statickým metodám třídy `Object` přes třídu `Králík`, například:
4141

4242
```js run
43-
class Rabbit extends Object {}
43+
class Králík extends Object {}
4444

4545
*!*
46-
// normally we call Object.getOwnPropertyNames
47-
alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // a,b
46+
// běžně voláme Object.getOwnPropertyNames
47+
alert ( Králík.getOwnPropertyNames({a: 1, b: 2})); // a,b
4848
*/!*
4949
```
5050

51-
But if we don't have `extends Object`, then `Rabbit.__proto__` is not set to `Object`.
51+
Jestliže však nemáme `extends Object`, pak se `Králík.__proto__` nenastaví na `Object`.
5252

53-
Here's the demo:
53+
Zde je ukázka:
5454

5555
```js run
56-
class Rabbit {}
56+
class Králík {}
5757

58-
alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
59-
alert( Rabbit.__proto__ === Object ); // (2) false (!)
60-
alert( Rabbit.__proto__ === Function.prototype ); // as any function by default
58+
alert( Králík.prototype.__proto__ === Object.prototype ); // (1) true
59+
alert( Králík.__proto__ === Object ); // (2) false (!)
60+
alert( Králík.__proto__ === Function.prototype ); // jako standardně kterákoli funkce
6161

6262
*!*
63-
// error, no such function in Rabbit
64-
alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error
63+
// chyba, ve třídě Králík taková funkce není
64+
alert ( Králík.getOwnPropertyNames({a: 1, b: 2})); // Chyba
6565
*/!*
6666
```
6767

68-
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
68+
`Králík` tedy v tomto případě neposkytuje přístup ke statickým metodám třídy `Object`.
6969

70-
By the way, `Function.prototype` also has "generic" function methods, like `call`, `bind` etc. They are ultimately available in both cases, because for the built-in `Object` constructor, `Object.__proto__ === Function.prototype`.
70+
Mimochodem, `Function.prototype` obsahuje „obecné“ funkční metody, např. `call`, `bind` atd. Ty jsou definitivně dostupné v obou případech, protože pro zabudovaný konstruktor třídy `Object` platí `Object.__proto__ === Function.prototype`.
7171

72-
Here's the picture:
72+
Zde je obrázek:
7373

7474
![](rabbit-extends-object.svg)
7575

76-
So, to put it short, there are two differences:
76+
Abychom to tedy zkrátili, existují dva rozdíly:
7777

78-
| class Rabbit | class Rabbit extends Object |
78+
| class Králík | class Králík extends Object |
7979
|--------------|------------------------------|
80-
| -- | needs to call `super()` in constructor |
81-
| `Rabbit.__proto__ === Function.prototype` | `Rabbit.__proto__ === Object` |
80+
| -- | musí v konstruktoru volat `super()` |
81+
| `Králík.__proto__ === Function.prototype` | `Králík.__proto__ === Object` |

1-js/09-classes/03-static-properties-methods/3-class-extend-object/task.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@ importance: 3
22

33
---
44

5-
# Class extends Object?
5+
# Třída rozšiřuje Object?
66

7-
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods like `hasOwnProperty` etc.
7+
Jak víme, všechny objekty běžně dědí z `Object.prototype` a získávají přístup k „obecným“ objektovým metodám jako `hasOwnProperty` a podobně.
88

9-
For instance:
9+
Například:
1010

1111
```js run
12-
class Rabbit {
13-
constructor(name) {
14-
this.name = name;
12+
class Králík {
13+
constructor(jméno) {
14+
this.jméno = jméno;
1515
}
1616
}
1717

18-
let rabbit = new Rabbit("Rab");
18+
let králík = new Králík("Bobek");
1919

2020
*!*
21-
// hasOwnProperty method is from Object.prototype
22-
alert( rabbit.hasOwnProperty('name') ); // true
21+
// metoda hasOwnProperty je z Object.prototype
22+
alert( králík.hasOwnProperty('jméno') ); // true
2323
*/!*
2424
```
2525

26-
But if we spell it out explicitly like `"class Rabbit extends Object"`, then the result would be different from a simple `"class Rabbit"`?
26+
Pokud však výslovně uvedeme `class Králík extends Object`, bude se výsledek lišit od prostého `class Králík“`?
2727

28-
What's the difference?
28+
Jaký je rozdíl?
2929

30-
Here's an example of such code (it doesn't work -- why? fix it?):
30+
Zde je příklad takového kódu (nefunguje -- proč? opravte ho):
3131

3232
```js
33-
class Rabbit extends Object {
34-
constructor(name) {
35-
this.name = name;
33+
class Králík extends Object {
34+
constructor(jméno) {
35+
this.jméno = jméno;
3636
}
3737
}
3838

39-
let rabbit = new Rabbit("Rab");
39+
let králík = new Králík("Bobek");
4040

41-
alert( rabbit.hasOwnProperty('name') ); // Error
41+
alert( králík.hasOwnProperty('jméno') ); // Chyba
4242
```

0 commit comments

Comments
 (0)