Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Prototypal inheritance#177
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 3 additions & 3 deletions1-js/08-prototypes/01-prototype-inheritance/1-property-after-delete/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
1. `true`,převezme se z `králík`. | ||
2. `null`,převezme se ze `zvíře`. | ||
3. `undefined`,taková vlastnost již neexistuje. |
28 changes: 14 additions & 14 deletions1-js/08-prototypes/01-prototype-inheritance/1-property-after-delete/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
36 changes: 18 additions & 18 deletions1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
1.Přidejme `__proto__`: | ||
```js run | ||
lethlava = { | ||
brýle: 1 | ||
}; | ||
letstůl = { | ||
pero: 3, | ||
__proto__:hlava | ||
}; | ||
letpostel = { | ||
peřina: 1, | ||
polštář: 2, | ||
__proto__:stůl | ||
}; | ||
letkapsy = { | ||
peníze: 2000, | ||
__proto__:postel | ||
}; | ||
alert(kapsy.pero ); // 3 | ||
alert(postel.brýle ); // 1 | ||
alert(stůl.peníze ); // undefined | ||
``` | ||
2.V moderních motorech s vylepšeným výkonem není rozdíl mezi tím, zda bereme vlastnost z objektu nebo jeho prototypu. Motory si pamatují, kde byla vlastnost nalezena, a při dalším požadavku to využijí. | ||
Například pro `kapsy.brýle` si pamatují, kde našly `brýle` (v objektu `hlava`),a příště budou hledat rovnou tam. Jsou také dostatečně chytré, aby si při nějaké změně své vnitřní mezipaměti aktualizovaly, takže tato optimalizace je bezpečná. |
28 changes: 14 additions & 14 deletions1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
**Odpověď zní: `králík`.** | ||
Je to proto, že`this`je objekt před tečkou, takže `králík.žer()`změní objekt `králík`. | ||
Hledání vlastnosti a spouštění metody jsou dvě různé věci. | ||
Metoda `králík.žer` je nejprve nalezena v prototypu a pak je spuštěna s`this=králík`. |
18 changes: 9 additions & 9 deletions1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
80 changes: 40 additions & 40 deletions1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,80 @@ | ||
Podívejme se pozorně na to, co se děje při volání `rychlý.žer("jablko")`. | ||
1.Metoda `rychlý.žer` je nalezena v prototypu(`=křeček`) a pak se spustí s`this=rychlý` (objekt před tečkou). | ||
2.Pak `this.žaludek.push()`musí najít vlastnost `žaludek` a zavolat na ní`push`. Hledá `žaludek` v `this` (`=rychlý`),ale nic nenajde. | ||
3.Pak následuje řetězec prototypů a najde `žaludek` v objektu `křeček`. | ||
4.Pak na něm volá`push`, čímž přidá potravu do *žaludku prototypu*. | ||
Všichni křečci tedy sdílejí jediný žaludek! | ||
Jak pro `líný.žaludek.push(...)`, tak pro `rychlý.žaludek.push()` je vlastnost `žaludek` nalezena v prototypu (protože není v samotném objektu) a pak jsou do ní vložena nová data. | ||
Prosíme všimněte si, že při jednoduchém přiřazení`this.žaludek=` se to nestane: | ||
```js run | ||
letkřeček = { | ||
žaludek: [], | ||
žer(potrava) { | ||
*!* | ||
//přiřazení do this.žaludek namístothis.žaludek.push | ||
this.žaludek = [potrava]; | ||
*/!* | ||
} | ||
}; | ||
letrychlý = { | ||
__proto__:křeček | ||
}; | ||
letlíný = { | ||
__proto__:křeček | ||
}; | ||
//Rychlý křeček našel potravu | ||
rychlý.žer("jablko"); | ||
alert(rychlý.žaludek ); //jablko | ||
//Žaludek líného křečka je prázdný | ||
alert(líný.žaludek ); // <nic> | ||
``` | ||
Teď vše funguje správně, protože `this.žaludek=`nehledá `žaludek`, ale hodnota se zapíše přímo do objektu`this`. | ||
Problému se můžeme zcela vyhnout i tak, že zajistíme, aby každý křeček měl svůj vlastní žaludek: | ||
```js run | ||
letkřeček = { | ||
žaludek: [], | ||
žer(potrava) { | ||
this.žaludek.push(potrava); | ||
} | ||
}; | ||
letrychlý = { | ||
__proto__:křeček, | ||
*!* | ||
žaludek: [] | ||
*/!* | ||
}; | ||
letlíný = { | ||
__proto__:křeček, | ||
*!* | ||
žaludek: [] | ||
*/!* | ||
}; | ||
//Rychlý křeček našel potravu | ||
rychlý.žer("jablko"); | ||
alert(rychlý.žaludek ); //jablko | ||
//Žaludek líného křečka je prázdný | ||
alert(líný.žaludek ); // <nic> | ||
``` | ||
Běžné řešení je, že všechny vlastnosti, které popisují stav určitého objektu, jako třeba uvedený `žaludek`, by měly být zapisovány přímo do tohoto objektu. Tím předejdeme takovýmto problémům. |
32 changes: 16 additions & 16 deletions1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.