Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Function binding#172
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
Function binding#172
Changes fromall commits
Commits
Show all changes
2 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
10 changes: 5 additions & 5 deletions1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/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,18 +1,18 @@ | ||
Odpověď: `null`. | ||
```js run | ||
function f() { | ||
alert( this ); // null | ||
} | ||
letuživatel = { | ||
g: f.bind(null) | ||
}; | ||
uživatel.g(); | ||
``` | ||
Kontext vázané funkce je napevno nastaven. Není žádný způsob, jak jej dále změnit. | ||
I když tedy spustíme `uživatel.g()`,původní funkce se volá s `this=null`. |
8 changes: 4 additions & 4 deletions1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/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
12 changes: 6 additions & 6 deletions1-js/06-advanced-functions/10-bind/3-second-bind/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,15 +1,15 @@ | ||
Odpověď: **Jan**. | ||
```js run no-beautify | ||
function f() { | ||
alert(this.jméno); | ||
} | ||
f = f.bind( {jméno: "Jan"} ).bind( {jméno: "Petr"} ); | ||
f(); //Jan | ||
``` | ||
Exotický objekt [vázané funkce](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects), vracený funkcí`f.bind(...)`, si pamatuje kontext (a argumenty, jsou-li uvedeny) jen v době vytvoření. | ||
Funkci nelze navázat znovu. |
10 changes: 5 additions & 5 deletions1-js/06-advanced-functions/10-bind/3-second-bind/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
4 changes: 2 additions & 2 deletions1-js/06-advanced-functions/10-bind/4-function-property-after-bind/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 @@ | ||
Odpověď: `undefined`. | ||
Výsledkem funkce`bind`je jiný objekt, který nemá vlastnost`test`. | ||
16 changes: 8 additions & 8 deletions1-js/06-advanced-functions/10-bind/4-function-property-after-bind/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
38 changes: 19 additions & 19 deletions1-js/06-advanced-functions/10-bind/5-question-use-bind/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,43 +1,43 @@ | ||
Chyba nastane proto, že funkce `zeptejSeNaHeslo` obdrží funkce `přihlášeníOK/přihlášeníSelhalo` bez objektu. | ||
Když je zavolá, přirozeně předpokládají `this=undefined`. | ||
Navažme kontext funkcí`bind`: | ||
```js run | ||
functionzeptejSeNaHeslo(ok,selhal) { | ||
letheslo = prompt("Heslo?", ''); | ||
if (heslo == "rockstar") ok(); | ||
elseselhal(); | ||
} | ||
letuživatel = { | ||
jméno: 'Jan', | ||
přihlášeníOK() { | ||
alert(`${this.jméno} se přihlásil`); | ||
}, | ||
přihlášeníSelhalo() { | ||
alert(`${this.jméno} se nedokázal přihlásit`); | ||
}, | ||
}; | ||
*!* | ||
zeptejSeNaHeslo(uživatel.přihlášeníOK.bind(uživatel),uživatel.přihlášeníSelhalo.bind(uživatel)); | ||
*/!* | ||
``` | ||
Teď to funguje. | ||
Alternativní řešení by mohlo být: | ||
```js | ||
//... | ||
zeptejSeNaHeslo(() =>uživatel.přihlášeníOK(), () =>uživatel.přihlášeníSelhalo()); | ||
``` | ||
Obvykle to také funguje a vypadá dobře. | ||
Je to však méně spolehlivé ve složitějších situacích, kdy se proměnná `uživatel` může změnit *po* volání `zeptejSeNaHeslo`, ale *před* odpovědí návštěvníka a voláním`() =>uživatel.přihlášeníOK()`. |
30 changes: 15 additions & 15 deletions1-js/06-advanced-functions/10-bind/5-question-use-bind/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
12 changes: 5 additions & 7 deletions1-js/06-advanced-functions/10-bind/6-ask-partial/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,16 +1,14 @@ | ||
1. Buď použijeme obalovou funkci, pro stručnost šipkovou: | ||
```js | ||
zeptejSeNaHeslo(() =>uživatel.přihlaš(true), () =>uživatel.přihlaš(false)); | ||
``` | ||
Nyní načte proměnnou `uživatel` z vnějších proměnných a funguje běžným způsobem. | ||
2. Nebo vytvoříme částečnou funkci z `uživatel.přihlaš`, která používá `uživatel` jako kontext a má příslušný první argument: | ||
```js | ||
zeptejSeNaHeslo(uživatel.přihlaš.bind(uživatel, true),uživatel.přihlaš.bind(uživatel, false)); | ||
``` |
28 changes: 14 additions & 14 deletions1-js/06-advanced-functions/10-bind/6-ask-partial/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.