You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, let's see why the latter code doesn't work.
1
+
Спочатку подивімося, чому останній код не працює.
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
+
Причина стає очевидною, якщо ми спробуємо його запустити. Конструктор класу, що успадковується, повинен викликати`super()`. Інакше`"this"`буде не визначене.
4
4
5
-
So here's the fix:
5
+
Виправлення:
6
6
7
7
```js run
8
8
classRabbitextendsObject {
9
9
constructor(name) {
10
10
*!*
11
-
super(); //need to call the parent constructor when inheriting
11
+
super(); //потрібно викликати батьківський конструктор під час успадкування
12
12
*/!*
13
13
this.name= name;
14
14
}
@@ -19,16 +19,16 @@ let rabbit = new Rabbit("Rab");
19
19
alert( rabbit.hasOwnProperty('name') ); // true
20
20
```
21
21
22
-
But that's not all yet.
22
+
Але це ще не все.
23
23
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`.
25
25
26
-
As we know, the "extends" syntax sets up two prototypes:
26
+
Як ми знаємо, синтаксис "extends" встановлює два прототипи:
27
27
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.Між самими функціями-конструкторами (для статичних методів).
30
30
31
-
In our case, for`class Rabbit extends Object`it means:
31
+
У нашому випадку для`class Rabbit extends Object`це означає:
So`Rabbit`doesn't provide access to static methods of `Object`in that case.
68
+
Тому`Rabbit`не надає доступу до статичних методів `Object`у цьому випадку.
69
69
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`.
71
71
72
-
Here's the picture:
72
+
Приклад на картинці:
73
73
74
74

75
75
76
-
So, to put it short, there are two differences:
76
+
Коротко кажучи, є дві відмінності:
77
77
78
78
| class Rabbit | class Rabbit extends Object |
79
79
|--------------|------------------------------|
80
-
| -- |needs to call `super()`in constructor|
80
+
| -- |необхідно викликати `super()`в конструкторі|
Copy file name to clipboardExpand all lines: 1-js/09-classes/03-static-properties-methods/3-class-extend-object/task.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@ importance: 3
2
2
3
3
---
4
4
5
-
# Class extends Object?
5
+
# Клас розширює об’єкт?
6
6
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`тощо.
8
8
9
-
For instance:
9
+
Наприклад:
10
10
11
11
```js run
12
12
classRabbit {
@@ -18,16 +18,16 @@ class Rabbit {
18
18
let rabbit =newRabbit("Rab");
19
19
20
20
*!*
21
-
// hasOwnProperty method is from Object.prototype
21
+
//метод hasOwnProperty від Object.prototype
22
22
alert( rabbit.hasOwnProperty('name') ); // true
23
23
*/!*
24
24
```
25
25
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"`?
27
27
28
-
What's the difference?
28
+
Яка різниця?
29
29
30
-
Here's an example of such code (it doesn't work -- why? fix it?):
30
+
Ось приклад такого коду (він не працює - чому? виправте його):
0 commit comments