Skip to content

Commit f9c04f3

Browse files
F.prototype (#194)
* Prototypal inheritance * Update 1-js/08-prototypes/01-prototype-inheritance/article.md * Prototypal inheritance with tasks * Update 1-js/08-prototypes/01-prototype-inheritance/article.md * Update 1-js/08-prototypes/01-prototype-inheritance/article.md * Update 1-js/08-prototypes/01-prototype-inheritance/article.md * Update 1-js/08-prototypes/01-prototype-inheritance/1-property-after-delete/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md * Update 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md * Update 1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md * Update 1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md * Update 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md * Corrections * Prototypal inheritance with bugsfix * Prototypal inheritance with bugsfix2 * Prototypal inheritance wit finel touch fix * F.prototype * Update 1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md * Update 1-js/08-prototypes/02-function-prototype/article.md * Update 1-js/08-prototypes/02-function-prototype/article.md * Update 1-js/08-prototypes/02-function-prototype/article.md * Update 1-js/08-prototypes/02-function-prototype/article.md Co-authored-by: Taras <[email protected]>
1 parent fe8e1a2 commit f9c04f3

File tree

7 files changed

+86
-86
lines changed

7 files changed

+86
-86
lines changed

1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ importance: 5
66

77
Ми маємо об’єкт `rabbit`, котрий успадковує властивості від об’єкта `animal`.
88

9-
Якщо ми викличемо `rabbit.eat()`, який з об’єктів буде записана властивість `full`: в `animal` чи `rabbit`?
9+
Якщо ми викличемо `rabbit.eat()`, у який з об’єктів буде записана властивість `full`: в `animal` чи `rabbit`?
1010

1111
```js
1212
let animal = {

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let hamster = {
2020

2121
eat(food) {
2222
*!*
23-
// визначається до this.stomach замість this.stomach.push
23+
// визначається до `this.stomach` замість `this.stomach.push`
2424
this.stomach = [food];
2525
*/!*
2626
}
@@ -39,7 +39,7 @@ speedy.eat("apple");
3939
alert( speedy.stomach ); // apple
4040

4141
// Шлунок хом’ячка 'Lazy' пустий
42-
alert( lazy.stomach ); // <nothing>
42+
alert( lazy.stomach ); // <нічого>
4343
```
4444

4545
Тепер все працює добре, тому що `this.stomach=` не виконує пошук властивості `stomach`. Значення записується прямо в `this` об’єкта.
@@ -69,12 +69,12 @@ let lazy = {
6969
*/!*
7070
};
7171

72-
// Хом’ячок 'Speedy' знайшов їду
73-
speedy.eat("apple");
74-
alert( speedy.stomach ); // apple
72+
// Хом’ячок `Speedy` знайшов їжу
73+
speedy.eat("яблуко");
74+
alert( speedy.stomach ); // яблуко
7575

76-
// Шлунок хом’ячка 'Lazy' пустий
77-
alert( lazy.stomach ); // <nothing>
76+
// Шлунок хом’ячка `Lazy` пустий
77+
alert( lazy.stomach ); // <нічого>
7878
```
7979

8080
Отже, спільним рішенням може бути те, що всі властивості, які описують стан конкретного об’єкта (подібно як `stomach`), повинні бути записані (визначені) в цьому ж самому об’єкті. Це уникне подібної проблеми.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
Answers:
2+
Відповідь:
33

44
1. `true`.
55

6-
The assignment to `Rabbit.prototype` sets up `[[Prototype]]` for new objects, but it does not affect the existing ones.
6+
Визначення `Rabbit.prototype` встановлює властивість `[[Prototype]]` для новоствореного об’єкта, але це жодним чином не впливає на вже існуючий об’єкт.
77

88
2. `false`.
99

10-
Objects are assigned by reference. The object from `Rabbit.prototype` is not duplicated, it's still a single object referenced both by `Rabbit.prototype` and by the `[[Prototype]]` of `rabbit`.
10+
Об’єкти призначаються шляхом посилання на них. Об’єкт з властивістю `Rabbit.prototype` не дублюється, це є той самий об’єкт на який посилаються як через `Rabbit.prototype` так і через властивість `[[Prototype]]` об’єкта `rabbit`.
1111

12-
So when we change its content through one reference, it is visible through the other one.
12+
А отже, коли ми змінюємо контент такого об’єкта через посилання, такі зміни стають видимі і через інші посилання.
1313

1414
3. `true`.
1515

16-
All `delete` operations are applied directly to the object. Here `delete rabbit.eats` tries to remove `eats` property from `rabbit`, but it doesn't have it. So the operation won't have any effect.
16+
Усі `delete` операції застосовуються безпосередньо на самому об'єкті. Тут `delete rabbit.eats` намагається видалити властивість `eats` з об’єкта `rabbit`, але такої властивості немає. А тому така операція не має ніякого ефекту.
1717

1818
4. `undefined`.
1919

20-
The property `eats` is deleted from the prototype, it doesn't exist any more.
20+
Властивість `eats` видалена з прототипу, вона більше не існує.

1-js/08-prototypes/02-function-prototype/1-changing-prototype/task.md

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

33
---
44

5-
# Changing "prototype"
5+
# Заміна властивості "prototype"
66

7-
In the code below we create `new Rabbit`, and then try to modify its prototype.
7+
В коді, що показаний нижче, ми створюємо об’єкт `new Rabbit` і потім міняємо його прототип.
88

9-
In the start, we have this code:
9+
На початку, маємо цей код:
1010

1111
```js run
1212
function Rabbit() {}
@@ -20,7 +20,7 @@ alert( rabbit.eats ); // true
2020
```
2121

2222

23-
1. We added one more string (emphasized). What will `alert` show now?
23+
1. Ми додали ще рядок коду (виділений). Що покаже тепер `alert`?
2424

2525
```js
2626
function Rabbit() {}
@@ -37,7 +37,7 @@ alert( rabbit.eats ); // true
3737
alert( rabbit.eats ); // ?
3838
```
3939

40-
2. ...And if the code is like this (replaced one line)?
40+
2. ...А якщо маємо такий код (з видаленим рядком коду)?
4141

4242
```js
4343
function Rabbit() {}
@@ -54,7 +54,7 @@ alert( rabbit.eats ); // true
5454
alert( rabbit.eats ); // ?
5555
```
5656

57-
3. And like this (replaced one line)?
57+
3. А якщо так (також з видаленим рядком коду)?
5858

5959
```js
6060
function Rabbit() {}
@@ -71,7 +71,7 @@ alert( rabbit.eats ); // true
7171
alert( rabbit.eats ); // ?
7272
```
7373

74-
4. The last variant:
74+
4. І останній варіант:
7575

7676
```js
7777
function Rabbit() {}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
We can use such approach if we are sure that `"constructor"` property has the correct value.
1+
Так, ми можемо використовувати такий підхід якщо ми впевнені, що властивість `"constructor"` має правильне значення.
22

3-
For instance, if we don't touch the default `"prototype"`, then this code works for sure:
3+
Для прикладу, якщо ми не чіпаємо властивість за замовчуванням `"prototype"`, тоді цей код буде працювати правильно:
44

55
```js run
66
function User(name) {
@@ -10,14 +10,14 @@ function User(name) {
1010
let user = new User('John');
1111
let user2 = new user.constructor('Pete');
1212

13-
alert( user2.name ); // Pete (worked!)
13+
alert( user2.name ); // Pete (працює!)
1414
```
1515

16-
It worked, because `User.prototype.constructor == User`.
16+
Код працює, тому що `User.prototype.constructor == User`.
1717

18-
..But if someone, so to speak, overwrites `User.prototype` and forgets to recreate `constructor` to reference `User`, then it would fail.
18+
..Але якщо хтось, якщо можна так виразитись, перезапише `User.prototype` і забуде додати властивість `constructor` в посиланні властивості об’єкта `User`, тоді цей код не буде працювати правильно.
1919

20-
For instance:
20+
Наприклад:
2121

2222
```js run
2323
function User(name) {
@@ -33,17 +33,17 @@ let user2 = new user.constructor('Pete');
3333
alert( user2.name ); // undefined
3434
```
3535

36-
Why `user2.name` is `undefined`?
36+
Чому `user2.name` є `undefined`?
3737

38-
Here's how `new user.constructor('Pete')` works:
38+
Ось тут пояснення як `new user.constructor('Pete')` працює:
3939

40-
1. First, it looks for `constructor` in `user`. Nothing.
41-
2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has no `constructor` (because we "forgot" to set it right!).
42-
3. Going further up the chain, `User.prototype` is a plain object, its prototype is the built-in `Object.prototype`.
43-
4. Finally, for the built-in `Object.prototype`, there's a built-in `Object.prototype.constructor == Object`. So it is used.
40+
1. Спочатку, здійснюється пошук у властивості `constructor` об’єкта `user`. Нічого не знаходять.
41+
2. Потім переключаються на ланцюжок прототипу. Прототипом для об’єкта `user` є `User.prototype`, і він також не має властивості `constructor` (тому що ми "забули" визначити його правильним чином!).
42+
3. Йдучи далі по ланцюжку прототипу, визначаємо, що `User.prototype` є простий об’єкт, його прототипом є вбудований глобальний `Object.prototype`.
43+
4. Врешті, для вбудованого `Object.prototype`, є вбудований конструктор глобального об’єкта `Object.prototype.constructor == Object` от він і використовується.
4444

45-
Finally, at the end, we have `let user2 = new Object('Pete')`.
45+
Таким чином, в кінці, ми отримуємо `let user2 = new Object('Pete')`.
4646

47-
Probably, that's not what we want. We'd like to create `new User`, not `new Object`. That's the outcome of the missing `constructor`.
47+
Ймовірно, це не те, що нам потрібно. Ми би хотіли стоврити `new User`, а не `new Object`. Це і є наслідки пропуску властивості `constructor`.
4848

49-
(Just in case you're curious, the `new Object(...)` call converts its argument to an object. That's a theoretical thing, in practice no one calls `new Object` with a value, and generally we don't use `new Object` to make objects at all).
49+
(на випадок, якщо вас зацікавить, виклик `new Object(...)` перетворює його аргументи на об’єкт. Це в теорії, але на практиці ніхто не викликає `new Object` з аргументами; і загалом, не використовується узагалі `new Object` для створення нових об’єктів).

1-js/08-prototypes/02-function-prototype/4-new-object-same-constructor/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ importance: 5
22

33
---
44

5-
# Create an object with the same constructor
5+
# Створення нового об’єкта з одинаковим конструктором
66

7-
Imagine, we have an arbitrary object `obj`, created by a constructor function -- we don't know which one, but we'd like to create a new object using it.
7+
Уявіть собі, у нас є довільний об’єкт `obj`, який створений функцією-конструктором -- ми не знаємо яким саме конструктором, але потрібно створити новий об’єкт використовуючи той самий конструктор.
88

9-
Can we do it like that?
9+
Чи можна створити новий об’єкт ось так?
1010

1111
```js
1212
let obj2 = new obj.constructor();
1313
```
1414

15-
Give an example of a constructor function for `obj` which lets such code work right. And an example that makes it work wrong.
15+
Покажіть приклад функції-конструктора для обєкта `obj`, який забезпечить правильну роботу такого коду, а також приклад, який, при такому коді, працює неправельно.

0 commit comments

Comments
 (0)