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__#180

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
otmon76 merged 3 commits intojavascript-tutorial:masterfromotmon76:1.8.4
Jun 4, 2025
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.
Metoda může vzít všechny enumerovatelné klíče pomocí`Object.keys`a vydat jejich seznam.

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.
Abychom učinili `toString`neenumerovatelnou, definujme ji pomocí deskriptoru vlastnosti. Syntaxe`Object.create`nám umožní jako druhý argument poskytnout objekt s deskriptory vlastností.

```js run
*!*
letdictionary = Object.create(null, {
toString: { //define toString property
value() { //the value is a function
letslovník = Object.create(null, {
toString: { //definujeme vlastnost toString
value() { //její hodnotou je funkce
return Object.keys(this).join();
}
}
});
*/!*

dictionary.apple = "Apple";
dictionary.__proto__ = "test";
slovník.jablko = "Jablko";
slovník.__proto__ = "test";

//apple and __proto__ is in the loop
for(letkey indictionary) {
alert(key); // "apple",then "__proto__"
//v cyklu jsou jablko a __proto__
for(letklíč inslovník) {
alert(klíč); // "jablko",pak "__proto__"
}

//comma-separated list of properties by toString
alert(dictionary); // "apple,__proto__"
//seznam vlastností oddělených čárkou, vydaný metodou toString
alert(slovník); // "jablko,__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.
Když vytvoříme vlastnost použitím deskriptoru, její přepínače jsou standardně`false`. V uvedeném kódu je tedy `slovník.toString`neenumerovatelná.

See the chapter[](info:property-descriptors) for review.
Pro přehled nahlédněte do kapitoly[](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
#Přidejte toStringdo slovníku

There's an object `dictionary`, created as `Object.create(null)`,to store any `key/value` pairs.
Máme objekt `slovník` vytvořený použitím `Object.create(null)`,do něhož ukládáme libovolné dvojice `klíč/hodnota`.

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.
Přidejte do něj metodu `slovník.toString()`, která by měla vracet seznam klíčů oddělených čárkou. Vaše metoda`toString`by se neměla ukazovat v cyklu`for..in`nad objektem.

Here's how it should work:
Takto by to mělo fungovat:

```js
letdictionary = Object.create(null);
letslovník = Object.create(null);

*!*
//your code to add dictionary.toString method
//váš kód přidávající metodu slovník.toString
*/!*

//add some data
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__is a regular property key here
//přidáme nějaká data
slovník.jablko = "Jablko";
slovník.__proto__ = "test"; // __proto__je zde klíč obvyklé vlastnosti

//only apple and __proto__ are in the loop
for(letkey indictionary) {
alert(key); // "apple",then "__proto__"
//v cyklu jsou jen jablko a __proto__
for(letklíč inslovník) {
alert(klíč); // "jablko",pak "__proto__"
}

//your toStringin action
alert(dictionary); // "apple,__proto__"
//váš toStringv akci
alert(slovník); // "jablko,__proto__"
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@

The first call has`this ==rabbit`,the other ones have`this`equal to `Rabbit.prototype`,because it's actually the object before the dot.
První volání má`this ==králík`,v ostatních je`this`rovno `Králík.prototype`,protože je to ve skutečnosti objekt před tečkou.

So only the first call shows `Rabbit`,other ones show `undefined`:
Jedině první volání tedy zobrazí `Králík`,ostatní zobrazí `undefined`:

```js run
functionRabbit(name) {
this.name =name;
functionKrálík(jméno) {
this.jméno =jméno;
}
Rabbit.prototype.sayHi = function() {
alert( this.name );
Králík.prototype.řekniAhoj = function() {
alert( this.jméno );
}

letrabbit = newRabbit("Rabbit");
letkrálík = newKrálík("Králík");

rabbit.sayHi(); //Rabbit
Rabbit.prototype.sayHi(); // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi(); // undefined
králík.řekniAhoj(); //Králík
Králík.prototype.řekniAhoj(); // undefined
Object.getPrototypeOf(králík).řekniAhoj(); // undefined
králík.__proto__.řekniAhoj(); // undefined
```
24 changes: 12 additions & 12 deletions1-js/08-prototypes/04-prototype-methods/3-compare-calls/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,26 +2,26 @@ importance: 5

---

#The difference between calls
#Rozdíl mezi voláními

Let's create a new `rabbit` object:
Vytvořme nový objekt `králík`:

```js
functionRabbit(name) {
this.name =name;
functionKrálík(jméno) {
this.jméno =jméno;
}
Rabbit.prototype.sayHi = function() {
alert(this.name);
Králík.prototype.řekniAhoj = function() {
alert(this.jméno);
};

letrabbit = newRabbit("Rabbit");
letkrálík = newKrálík("Králík");
```

These calls do the same thing or not?
Učiní všechna tato volání totéž, nebo ne?

```js
rabbit.sayHi();
Rabbit.prototype.sayHi();
Object.getPrototypeOf(rabbit).sayHi();
rabbit.__proto__.sayHi();
králík.řekniAhoj();
Králík.prototype.řekniAhoj();
Object.getPrototypeOf(králík).řekniAhoj();
králík.__proto__.řekniAhoj();
```
Loading

[8]ページ先頭

©2009-2025 Movatter.jp