Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Object methods, "this"#150
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
+251 −247
Merged
Changes fromall commits
Commits
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
46 changes: 23 additions & 23 deletions1-js/04-object-basics/04-object-methods/4-object-property-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,55 +1,55 @@ | ||
**Odpověď: chyba.** | ||
Zkuste to: | ||
```js run | ||
functionvytvořUživatele() { | ||
return { | ||
jméno: "Jan", | ||
odkaz: this | ||
}; | ||
} | ||
letuživatel =vytvořUživatele(); | ||
alert(uživatel.odkaz.jméno ); //Chyba: Nelze načíst vlastnost 'jméno' objektu undefined | ||
``` | ||
Je to proto, že pravidla, která nastavují`this`, se nedívají do definice objektu. Záleží jen na okamžiku volání. | ||
Zde je hodnota`this`uvnitř `vytvořUživatele()` `undefined`,protože tato funkce je volána jako funkce, ne jako metoda „tečkovou“ syntaxí. | ||
Hodnota`this`je stejná pro celou funkci, bloky kódu a objektové literály ji neovlivňují. | ||
Takže `odkaz: this`vezme ve skutečnosti aktuální`this`této funkce. | ||
Můžeme funkci přepsat a vrátit stejné`this`s hodnotou`undefined`: | ||
```js run | ||
functionvytvořUživatele(){ | ||
return this; //tentokrát tady není objektový literál | ||
} | ||
alert(vytvořUživatele().jméno ); //Chyba: Nelze načíst vlastnost 'jméno' objektu undefined | ||
``` | ||
Jak vidíte, výsledek`alert(vytvořUživatele().jméno )`je stejný jako výsledek`alert(uživatel.odkaz.jméno )`z předchozího příkladu. | ||
Toto je opačný příklad: | ||
```js run | ||
functionvytvořUživatele() { | ||
return { | ||
jméno: "Jan", | ||
*!* | ||
odkaz() { | ||
return this; | ||
} | ||
*/!* | ||
}; | ||
} | ||
letuživatel =vytvořUživatele(); | ||
alert(uživatel.odkaz().jméno ); //Jan | ||
``` | ||
Teď to funguje, protože `uživatel.odkaz()`je metoda. A hodnota`this`se nastaví na objekt před tečkou `.`. |
16 changes: 8 additions & 8 deletions1-js/04-object-basics/04-object-methods/4-object-property-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
8 changes: 4 additions & 4 deletions1-js/04-object-basics/04-object-methods/7-calculator/_js.view/solution.js
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
21 changes: 10 additions & 11 deletions1-js/04-object-basics/04-object-methods/7-calculator/_js.view/test.js
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
14 changes: 7 additions & 7 deletions1-js/04-object-basics/04-object-methods/7-calculator/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,21 +1,21 @@ | ||
```js run demo solution | ||
letkalkulátor = { | ||
součet() { | ||
return this.a + this.b; | ||
}, | ||
součin() { | ||
return this.a * this.b; | ||
}, | ||
načti() { | ||
this.a = +prompt('a?', 0); | ||
this.b = +prompt('b?', 0); | ||
} | ||
}; | ||
kalkulátor.načti(); | ||
alert(kalkulátor.součet() ); | ||
alert(kalkulátor.součin() ); | ||
``` |
20 changes: 10 additions & 10 deletions1-js/04-object-basics/04-object-methods/7-calculator/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
17 changes: 11 additions & 6 deletions1-js/04-object-basics/04-object-methods/8-chain-calls/_js.view/solution.js
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,16 +1,21 @@ | ||
letžebřík = { | ||
stupeň: 0, | ||
nahoru: function() { | ||
this.stupeň++; | ||
return this; | ||
}, | ||
dolů: function() { | ||
this.stupeň--; | ||
return this; | ||
}, | ||
<<<<<<< HEAD | ||
zobrazStupeň: function() { | ||
alert(this.stupeň); | ||
======= | ||
showStep: function() { | ||
alert(this.step); | ||
return this; | ||
>>>>>>> 71da17e5960f1c76aad0d04d21f10bc65318d3f6 | ||
} | ||
}; |
30 changes: 15 additions & 15 deletions1-js/04-object-basics/04-object-methods/8-chain-calls/_js.view/test.js
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/04-object-basics/04-object-methods/8-chain-calls/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,39 +1,39 @@ | ||
Řešením je v každém volání vrátit tento objekt samotný. | ||
```js run demo | ||
letžebřík = { | ||
stupeň: 0, | ||
nahoru() { | ||
this.stupeň++; | ||
*!* | ||
return this; | ||
*/!* | ||
}, | ||
dolů() { | ||
this.stupeň--; | ||
*!* | ||
return this; | ||
*/!* | ||
}, | ||
zobrazStupeň() { | ||
alert( this.stupeň ); | ||
*!* | ||
return this; | ||
*/!* | ||
} | ||
}; | ||
žebřík.nahoru().nahoru().dolů().zobrazStupeň().dolů().zobrazStupeň(); //zobrazí 1, pak 0 | ||
``` | ||
Můžeme také psát každé volání na nový řádek. U delšího zřetězení je to čitelnější: | ||
```js | ||
žebřík | ||
.nahoru() | ||
.nahoru() | ||
.dolů() | ||
.zobrazStupeň() // 1 | ||
.dolů() | ||
.zobrazStupeň(); // 0 | ||
``` |
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.