Skip to content

Commit b471b1f

Browse files
authored
Prototype methods, objects without proto (#215)
1 parent 88c2c40 commit b471b1f

File tree

5 files changed

+104
-104
lines changed

5 files changed

+104
-104
lines changed
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11

2-
The method can take all enumerable keys using `Object.keys` and output their list.
2+
Метод може взяти всі перелічувані ключі об’єкта за допомогою `Object.keys` та вивести їх перелік.
33

4-
To make `toString` non-enumerable, let's define it using a property descriptor. The syntax of `Object.create` allows us to provide an object with property descriptors as the second argument.
4+
Для того щоб зробити метод `toString` не перлічуваним, визначимо його використовуючи дескриптор властивості. Синтаксис `Object.create` дозволяє нам надати об’єкту дескриптори властивостей як другий аргумент.
55

66
```js run
77
*!*
88
let dictionary = Object.create(null, {
9-
toString: { // define toString property
10-
value() { // the value is a function
9+
toString: { // визначаємо властивість toString
10+
value() { // value є функцією
1111
return Object.keys(this).join();
1212
}
1313
}
1414
});
1515
*/!*
1616

17-
dictionary.apple = "Apple";
18-
dictionary.__proto__ = "test";
17+
dictionary.apple = "Яблуко";
18+
dictionary.__proto__ = "тест";
1919

20-
// apple and __proto__ is in the loop
20+
// apple та __proto__ показуються в циклі
2121
for(let key in dictionary) {
22-
alert(key); // "apple", then "__proto__"
22+
alert(key); // "apple", потім "__proto__"
2323
}
2424

25-
// comma-separated list of properties by toString
25+
// метод toString повертає перелік властивостей через кому
2626
alert(dictionary); // "apple,__proto__"
2727
```
2828

29-
When we create a property using a descriptor, its flags are `false` by default. So in the code above, `dictionary.toString` is non-enumerable.
29+
Коли ми створюємо властивість використовуючи дескриптор, його опції мають значення `false` за замовчуванням. Тому в коді вище `dictionary.toString` є не перелічуваним.
3030

31-
See the the chapter [](info:property-descriptors) for review.
31+
Продивіться розділ [](info:property-descriptors).

1-js/08-prototypes/04-prototype-methods/2-dictionary-tostring/task.md

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

33
---
44

5-
# Add toString to the dictionary
5+
# Додайте toString до об’єкту-словника
66

7-
There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
7+
Дано об’єкт `dictionary`, створений за допомогою `Object.create(null)`, щоб зберегти будь-які пари `ключ/значення`.
88

9-
Add method `dictionary.toString()` into it, that should return a comma-delimited list of keys. Your `toString` should not show up in `for..in` over the object.
9+
Додайте метод `dictionary.toString()` до об’єкта, який повертає перелік ключів через кому. Метод `toString` не повинен показуватися, якщо застосувати до об’єкта цикл `for..in`.
1010

11-
Here's how it should work:
11+
Тут показано як це має працювати:
1212

1313
```js
1414
let dictionary = Object.create(null);
1515

1616
*!*
17-
// your code to add dictionary.toString method
17+
// ваш код, щоб додати dictionary.toString метод
1818
*/!*
1919

20-
// add some data
21-
dictionary.apple = "Apple";
22-
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
20+
// додаємо певні дані
21+
dictionary.apple = "Яблуко";
22+
dictionary.__proto__ = "тест"; // __proto__ тут є звичайною властивістю об’єкта
2323

24-
// only apple and __proto__ are in the loop
24+
// тільки ключі apple та __proto__ показуються в циклі
2525
for(let key in dictionary) {
26-
alert(key); // "apple", then "__proto__"
26+
alert(key); // "apple", потім "__proto__"
2727
}
2828

29-
// your toString in action
29+
// ваш метод toString в дії
3030
alert(dictionary); // "apple,__proto__"
3131
```

1-js/08-prototypes/04-prototype-methods/3-compare-calls/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
The first call has `this == rabbit`, the other ones have `this` equal to `Rabbit.prototype`, because it's actually the object before the dot.
2+
Перший виклик має `this == rabbit`, інші мають `this` рівний `Rabbit.prototype`, тому що це об’єкт перед крапкою.
33

4-
So only the first call shows `Rabbit`, other ones show `undefined`:
4+
Таким чином тільки перший виклик покаже `Кріль`, інші покажуть `undefined`:
55

66
```js run
77
function Rabbit(name) {
@@ -11,9 +11,9 @@ Rabbit.prototype.sayHi = function() {
1111
alert( this.name );
1212
}
1313

14-
let rabbit = new Rabbit("Rabbit");
14+
let rabbit = new Rabbit("Кріль");
1515

16-
rabbit.sayHi(); // Rabbit
16+
rabbit.sayHi(); // Кріль
1717
Rabbit.prototype.sayHi(); // undefined
1818
Object.getPrototypeOf(rabbit).sayHi(); // undefined
1919
rabbit.__proto__.sayHi(); // undefined

1-js/08-prototypes/04-prototype-methods/3-compare-calls/task.md

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

33
---
44

5-
# The difference between calls
5+
# Різниця між викликами
66

7-
Let's create a new `rabbit` object:
7+
Створимо новий об’єкт `rabbit`:
88

99
```js
1010
function Rabbit(name) {
@@ -14,10 +14,10 @@ Rabbit.prototype.sayHi = function() {
1414
alert(this.name);
1515
};
1616

17-
let rabbit = new Rabbit("Rabbit");
17+
let rabbit = new Rabbit("Кріль");
1818
```
1919

20-
These calls do the same thing or not?
20+
Чи виконують виклики нижче однакову дію чи ні?
2121

2222
```js
2323
rabbit.sayHi();

0 commit comments

Comments
 (0)