Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
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
View file
Open in desktop
Original file line numberDiff line numberDiff 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() { //thevalueis a function
toString: { //визначаємо властивість toString
value() { // valueє функцією
return Object.keys(this).join();
}
}
});
*/!*

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

// appleand __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).
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,30 +2,30 @@ importance: 5

---

#Add toStringto 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.toStringmethod
//ваш код, щоб додати dictionary.toStringметод
*/!*

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

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

//yourtoStringin action
//ваш методtoStringв дії
alert(dictionary); // "apple,__proto__"
```
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp