|
1 |
| -First, let's see why the latter code doesn't work. |
| 1 | +Nejprve se podívejme, proč poslední uvedený kód nefunguje. |
2 | 2 |
|
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“`. |
4 | 4 |
|
5 |
| -So here's the fix: |
| 5 | +Zde je tedy oprava: |
6 | 6 |
|
7 | 7 | ```js run
|
8 |
| -class Rabbit extends Object { |
9 |
| - constructor(name) { |
| 8 | +class Králík extends Object { |
| 9 | + constructor(jméno) { |
10 | 10 | *!*
|
11 |
| - super(); // need to call the parent constructor when inheriting |
| 11 | + super(); // při dědění je nutné volat rodičovský konstruktor |
12 | 12 | */!*
|
13 |
| - this.name = name; |
| 13 | + this.jméno = jméno; |
14 | 14 | }
|
15 | 15 | }
|
16 | 16 |
|
17 |
| -let rabbit = new Rabbit("Rab"); |
| 17 | +let králík = new Králík("Bobek"); |
18 | 18 |
|
19 |
| -alert( rabbit.hasOwnProperty('name') ); // true |
| 19 | +alert( králík.hasOwnProperty('jméno') ); // true |
20 | 20 | ```
|
21 | 21 |
|
22 |
| -But that's not all yet. |
| 22 | +To však ještě není všechno. |
23 | 23 |
|
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`. |
25 | 25 |
|
26 |
| -As we know, the "extends" syntax sets up two prototypes: |
| 26 | +Jak víme, syntaxe „extends“ nastavuje dva prototypy: |
27 | 27 |
|
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). |
30 | 30 |
|
31 |
| -In the case of `class Rabbit extends Object` it means: |
| 31 | +V případě `class Králík extends Object` to znamená: |
32 | 32 |
|
33 | 33 | ```js run
|
34 |
| -class Rabbit extends Object {} |
| 34 | +class Králík extends Object {} |
35 | 35 |
|
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 |
38 | 38 | ```
|
39 | 39 |
|
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: |
41 | 41 |
|
42 | 42 | ```js run
|
43 |
| -class Rabbit extends Object {} |
| 43 | +class Králík extends Object {} |
44 | 44 |
|
45 | 45 | *!*
|
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 |
48 | 48 | */!*
|
49 | 49 | ```
|
50 | 50 |
|
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`. |
52 | 52 |
|
53 |
| -Here's the demo: |
| 53 | +Zde je ukázka: |
54 | 54 |
|
55 | 55 | ```js run
|
56 |
| -class Rabbit {} |
| 56 | +class Králík {} |
57 | 57 |
|
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 |
61 | 61 |
|
62 | 62 | *!*
|
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 |
65 | 65 | */!*
|
66 | 66 | ```
|
67 | 67 |
|
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`. |
69 | 69 |
|
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`. |
71 | 71 |
|
72 |
| -Here's the picture: |
| 72 | +Zde je obrázek: |
73 | 73 |
|
74 | 74 | 
|
75 | 75 |
|
76 |
| -So, to put it short, there are two differences: |
| 76 | +Abychom to tedy zkrátili, existují dva rozdíly: |
77 | 77 |
|
78 |
| -| class Rabbit | class Rabbit extends Object | |
| 78 | +| class Králík | class Králík extends Object | |
79 | 79 | |--------------|------------------------------|
|
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` | |
0 commit comments