Skip to content

Commit 02923d4

Browse files
authored
Static properties and methods (#212)
* Static properties and methods
1 parent fb297b4 commit 02923d4

File tree

4 files changed

+85
-85
lines changed

4 files changed

+85
-85
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
First, let's see why the latter code doesn't work.
1+
Спочатку подивімося, чому останній код не працює.
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+
Причина стає очевидною, якщо ми спробуємо його запустити. Конструктор класу, що успадковується, повинен викликати `super()`. Інакше `"this"` буде не визначене.
44

5-
So here's the fix:
5+
Виправлення:
66

77
```js run
88
class Rabbit extends Object {
99
constructor(name) {
1010
*!*
11-
super(); // need to call the parent constructor when inheriting
11+
super(); // потрібно викликати батьківський конструктор під час успадкування
1212
*/!*
1313
this.name = name;
1414
}
@@ -19,16 +19,16 @@ let rabbit = new Rabbit("Rab");
1919
alert( rabbit.hasOwnProperty('name') ); // true
2020
```
2121

22-
But that's not all yet.
22+
Але це ще не все.
2323

24-
Even after the fix, there's still important difference in `"class Rabbit extends Object"` versus `class Rabbit`.
24+
Навіть після виправлення все ще існує важлива різниця між `"class Rabbit extends Object"` та `class Rabbit`.
2525

26-
As we know, the "extends" syntax sets up two prototypes:
26+
Як ми знаємо, синтаксис "extends" встановлює два прототипи:
2727

28-
1. Between `"prototype"` of the constructor functions (for methods).
29-
2. Between the constructor functions themselves (for static methods).
28+
1. Між `"prototype"` функцій-конструкторів (для методів).
29+
2. Між самими функціями-конструкторами (для статичних методів).
3030

31-
In our case, for `class Rabbit extends Object` it means:
31+
У нашому випадку для `class Rabbit extends Object` це означає:
3232

3333
```js run
3434
class Rabbit extends Object {}
@@ -37,45 +37,45 @@ alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
3737
alert( Rabbit.__proto__ === Object ); // (2) true
3838
```
3939

40-
So `Rabbit` now provides access to static methods of `Object` via `Rabbit`, like this:
40+
Отже, `Rabbit` тепер надає доступ до статичних методів `Object` через `Rabbit`, наприклад:
4141

4242
```js run
4343
class Rabbit extends Object {}
4444

4545
*!*
46-
// normally we call Object.getOwnPropertyNames
46+
// зазвичай ми викликаємо Object.getOwnPropertyNames
4747
alert ( Rabbit.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+
Але якщо у нас немає `extends Object`, тоді для `Rabbit.__proto__` не встановлено значення `Object`.
5252

53-
Here's the demo:
53+
Приклад:
5454

5555
```js run
5656
class Rabbit {}
5757

5858
alert( Rabbit.prototype.__proto__ === Object.prototype ); // (1) true
5959
alert( Rabbit.__proto__ === Object ); // (2) false (!)
60-
alert( Rabbit.__proto__ === Function.prototype ); // as any function by default
60+
alert( Rabbit.__proto__ === Function.prototype ); // як у будь-якої функції за замовчуванням
6161

6262
*!*
63-
// error, no such function in Rabbit
63+
// помилка, такої функції в Rabbit немає
6464
alert ( Rabbit.getOwnPropertyNames({a: 1, b: 2})); // Error
6565
*/!*
6666
```
6767

68-
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
68+
Тому `Rabbit` не надає доступу до статичних методів `Object` у цьому випадку.
6969

70-
By the way, `Function.prototype` 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+
До речі, `Function.prototype` має "загальні" методи функції, такі як `call`, `bind` тощо. Вони в кінцевому підсумку доступні в обох випадках, тому що для вбудованого конструктора `Object`, `Object.__proto__ === Function.prototype`.
7171

72-
Here's the picture:
72+
Приклад на картинці:
7373

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

76-
So, to put it short, there are two differences:
76+
Коротко кажучи, є дві відмінності:
7777

7878
| class Rabbit | class Rabbit extends Object |
7979
|--------------|------------------------------|
80-
| -- | needs to call `super()` in constructor |
80+
| -- | необхідно викликати `super()` в конструкторі |
8181
| `Rabbit.__proto__ === Function.prototype` | `Rabbit.__proto__ === Object` |

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

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

33
---
44

5-
# Class extends Object?
5+
# Клас розширює об’єкт?
66

7-
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods like `hasOwnProperty` etc.
7+
Як ми знаємо, всі об’єкти зазвичай успадковуються від `Object.prototype` й отримують доступ до «загальних» методів об’єкта, наприклад `hasOwnProperty` тощо.
88

9-
For instance:
9+
Наприклад:
1010

1111
```js run
1212
class Rabbit {
@@ -18,16 +18,16 @@ class Rabbit {
1818
let rabbit = new Rabbit("Rab");
1919

2020
*!*
21-
// hasOwnProperty method is from Object.prototype
21+
// метод hasOwnProperty від Object.prototype
2222
alert( rabbit.hasOwnProperty('name') ); // 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+
Але що як ми пропишемо явно `"class Rabbit extends Object"` - тоді результат буде відрізнятися від звичайного `"class Rabbit"`?
2727

28-
What's the difference?
28+
Яка різниця?
2929

30-
Here's an example of such code (it doesn't work -- why? fix it?):
30+
Ось приклад такого коду (він не працює - чому? виправте його):
3131

3232
```js
3333
class Rabbit extends Object {
Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)