Skip to content

Prototype methods, objects without proto #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

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

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.
Для того щоб зробити метод `toString` не перлічуваним, визначимо його використовуючи дескриптор властивості. Синтаксис `Object.create` дозволяє нам надати об’єкту дескриптори властивостей як другий аргумент.

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: { // визначаємо властивість toString
value() { // value є функцією
return Object.keys(this).join();
}
}
});
*/!*

dictionary.apple = "Apple";
dictionary.__proto__ = "test";
dictionary.apple = "Яблуко";
dictionary.__proto__ = "тест";

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

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

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

See the the chapter [](info:property-descriptors) for review.
Продивіться розділ [](info:property-descriptors).
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

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

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

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.
Додайте метод `dictionary.toString()` до об’єкта, який повертає перелік ключів через кому. Метод `toString` не повинен показуватися, якщо застосувати до об’єкта цикл `for..in`.

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

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

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

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

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

// your toString in action
// ваш метод toString в дії
alert(dictionary); // "apple,__proto__"
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

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

```js run
function Rabbit(name) {
Expand All @@ -11,9 +11,9 @@ Rabbit.prototype.sayHi = function() {
alert( this.name );
}

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("Кріль");

rabbit.sayHi(); // Rabbit
rabbit.sayHi(); // Кріль
Rabbit.prototype.sayHi(); // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi(); // undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

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

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

```js
function Rabbit(name) {
Expand All @@ -14,10 +14,10 @@ Rabbit.prototype.sayHi = function() {
alert(this.name);
};

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("Кріль");
```

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

```js
rabbit.sayHi();
Expand Down
Loading