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

F.prototype#178

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 1 commit intojavascript-tutorial:masterfromotmon76:1.8.2
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,20 +1,20 @@

Answers:
Odpovědi:

1. `true`.

The assignment to `Rabbit.prototype`sets up`[[Prototype]]`for new objects, but it does not affect the existing ones.
Přiřazení do `Králík.prototype`nastaví`[[Prototype]]`pro nové objekty, ale neovlivní již existující.

2. `false`.

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`.
Objekty jsou přiřazovány odkazem. Objekt z `Králík.prototype`není duplikován, ale je to stále jediný objekt, na který se odkazují jak `Králík.prototype`, tak`[[Prototype]]`objektu `králík`.

So when we change its content through one reference, it is visible through the other one.
Když tedy změníme jeho obsah skrz jeden odkaz, uvidíme to i druhým odkazem.

3. `true`.

All`delete`operations are applied directly to the object. Here`deleterabbit.eats` tries to remove `eats` property from `rabbit`,but it doesn't have it. So the operation won't have any effect.
Všechny operace`delete`se aplikují přímo na objekt. Zde se`deletekrálík.žere` pokusí odstranit vlastnost `žere` z objektu `králík`,ale ten ji neobsahuje. Operace tedy nemá žádný účinek.

4. `undefined`.

The property `eats` is deleted from the prototype, it doesn't exist any more.
Vlastnost `žere` je smazána z prototypu a nadále neexistuje.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,88 +2,88 @@ importance: 5

---

#Changing "prototype"
#Změna „prototype

In the code below we create`newRabbit`, and then try to modify its prototype.
V následujícím kódu vytvoříme`newKrálík` a pak se pokusíme změnit jeho prototyp.

In the start, we have this code:
Na začátku máme tento kód:

```js run
functionRabbit() {}
Rabbit.prototype = {
eats: true
functionKrálík() {}
Králík.prototype = {
žere: true
};

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

alert(rabbit.eats ); // true
alert(králík.žere ); // true
```


1.We added one more string (emphasized).What will`alert` show now?
1.Přidáme další řádek (zvýrazněný).Co nyní zobrazí`alert`?

```js
functionRabbit() {}
Rabbit.prototype = {
eats: true
functionKrálík() {}
Králík.prototype = {
žere: true
};

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

*!*
Rabbit.prototype = {};
Králík.prototype = {};
*/!*

alert(rabbit.eats ); // ?
alert(králík.žere ); // ?
```

2. ...And if the code is like this (replaced one line)?
2. ...A když kód vypadá takto (jeden řádek změněn)?

```js
functionRabbit() {}
Rabbit.prototype = {
eats: true
functionKrálík() {}
Králík.prototype = {
žere: true
};

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

*!*
Rabbit.prototype.eats = false;
Králík.prototype.žere = false;
*/!*

alert(rabbit.eats ); // ?
alert(králík.žere ); // ?
```

3.And like this (replaced one line)?
3.A takto (jeden řádek změněn)?

```js
functionRabbit() {}
Rabbit.prototype = {
eats: true
functionKrálík() {}
Králík.prototype = {
žere: true
};

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

*!*
deleterabbit.eats;
deletekrálík.žere;
*/!*

alert(rabbit.eats ); // ?
alert(králík.žere ); // ?
```

4.The last variant:
4.Poslední varianta:

```js
functionRabbit() {}
Rabbit.prototype = {
eats: true
functionKrálík() {}
Králík.prototype = {
žere: true
};

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

*!*
deleteRabbit.prototype.eats;
deleteKrálík.prototype.žere;
*/!*

alert(rabbit.eats ); // ?
alert(králík.žere ); // ?
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
We can use such approach if we are sure that `"constructor"`property has the correct value.
Tento přístup můžeme použít, pokud jsme si jisti, že vlastnost `"constructor"`obsahuje správnou hodnotu.

For instance, if we don't touch the default`"prototype"`,then this code works for sure:
Například pokud se nedotkneme standardní vlastnosti`"prototype"`,pak bude tento kód zaručeně fungovat:

```js run
functionUser(name) {
this.name =name;
functionUživatel(jméno) {
this.jméno =jméno;
}

letuser = newUser('John');
letuser2 = newuser.constructor('Pete');
letuživatel = newUživatel('Jan');
letuživatel2 = newuživatel.constructor('Petr');

alert(user2.name ); //Pete (worked!)
alert(uživatel2.jméno ); //Petr (funguje!)
```

It worked, because `User.prototype.constructor ==User`.
Funguje to, protože `Uživatel.prototype.constructor ==Uživatel`.

..But if someone, so to speak, overwrites `User.prototype`and forgets to recreate `constructor` to reference `User`,then it would fail.
...Ale pokud někdo dejme tomu přepíše `Uživatel.prototype`a zapomene znovu vytvořit `constructor`, aby odkazoval na `Uživatel`,pak to selže.

For instance:
Například:

```js run
functionUser(name) {
this.name =name;
functionUživatel(jméno) {
this.jméno =jméno;
}
*!*
User.prototype = {}; // (*)
Uživatel.prototype = {}; // (*)
*/!*

letuser = newUser('John');
letuser2 = newuser.constructor('Pete');
letuživatel = newUživatel('Jan');
letuživatel2 = newuživatel.constructor('Petr');

alert(user2.name ); // undefined
alert(uživatel2.jméno ); // undefined
```

Why `user2.name` is `undefined`?
Proč je `uživatel2.jméno` `undefined`?

Here's how `newuser.constructor('Pete')` works:
Takto funguje `newuživatel.constructor('Petr')`:

1.First, it looks for`constructor`in `user`.Nothing.
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!).
3.Going further up the chain, `User.prototype`is a plain object, its prototype is the built-in `Object.prototype`.
4.Finally, for the built-in`Object.prototype`, there's a built-in`Object.prototype.constructor == Object`.So it is used.
1.Nejprve hledá`constructor`v objektu `uživatel`.Nic.
2.Pak postupuje řetězcem prototypů. Prototyp objektu `uživatel` je `Uživatel.prototype` a ani ten nemá žádný `constructor` (protože jsme ho „zapomněli“ správně nastavit!).
3.Postupuje řetězcem dále nahoru. `Uživatel.prototype`je planý objekt a jeho prototypem je vestavěný `Object.prototype`.
4.Nakonec pro vestavěný`Object.prototype` je vestavěný`Object.prototype.constructor == Object`.Ten se tedy použije.

Finally, at the end, we have`letuser2 = new Object('Pete')`.
Nakonec tedy máme`letuživatel2 = new Object('Petr')`.

Probably, that's not what we want. We'd like to create`newUser`,not `new Object`.That's the outcome of the missing `constructor`.
To pravděpodobně není to, co chceme. Rádi bychom vytvořili`newUživatel`,ne `new Object`.To je důsledek chybějící vlastnosti `constructor`.

(Just in case you're curious, the`new Object(...)`call converts itsargumentto 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).
(Jen pro případ, že vás to zajímá: volání`new Object(...)`konvertuje svůjargumentna objekt. To je teoretická záležitost, v praxi nikdo nevolá`new Object`s hodnotou a obecně vůbec nepoužíváme`new Object`k vytváření objektů.)
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,14 +2,14 @@ importance: 5

---

#Create an object with the same constructor
#Vytvoření objektu stejným konstruktorem

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.
Představme si, že máme libovolný objekt`obj`,který je vytvořen konstruktorem--nevíme jakým, ale rádi bychom pomocí něj vytvořili nový objekt.

Can we do it like that?
Můžeme to udělat takto?

```js
let obj2 = new obj.constructor();
```

Give an example of a constructor function for`obj` which lets such code work right. And an example that makes it work wrong.
Uveďte příklad konstruktoru pro`obj`, pro který by takový kód správně fungoval. A příklad, pro který by fungoval špatně.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp