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

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
otmon76 merged 2 commits intojavascript-tutorial:masterfromotmon76:1.6.10
Jun 3, 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,18 +1,18 @@
The answer: `null`.
Odpověď: `null`.


```js run
function f() {
alert( this ); // null
}

letuser = {
letuživatel = {
g: f.bind(null)
};

user.g();
uživatel.g();
```

The context of a bound function is hard-fixed. There's just no way to further change it.
Kontext vázané funkce je napevno nastaven. Není žádný způsob, jak jej dále změnit.

So even while we run `user.g()`,the original function is called with `this=null`.
I když tedy spustíme `uživatel.g()`,původní funkce se volá s `this=null`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,19 +2,19 @@ importance: 5

---

#Bound function as a method
#Vázaná funkce jako metoda

What will be the output?
Jaký bude výstup?

```js
function f() {
alert( this ); // ?
}

letuser = {
letuživatel = {
g: f.bind(null)
};

user.g();
uživatel.g();
```

12 changes: 6 additions & 6 deletions1-js/06-advanced-functions/10-bind/3-second-bind/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
The answer: **John**.
Odpověď: **Jan**.

```js run no-beautify
function f() {
alert(this.name);
alert(this.jméno);
}

f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
f = f.bind( {jméno: "Jan"} ).bind( {jméno: "Petr"} );

f(); //John
f(); //Jan
```

The exotic [bound function](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) object returned by`f.bind(...)` remembers the context (and arguments if provided) only at creation time.
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í.

A function cannot be re-bound.
Funkci nelze navázat znovu.
10 changes: 5 additions & 5 deletions1-js/06-advanced-functions/10-bind/3-second-bind/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,18 +2,18 @@ importance: 5

---

#Second bind
#Druhá vazba

Can we change`this`by additional binding?
Můžeme změnit`this`další vazbou?

What will be the output?
Jaký bude výstup?

```js no-beautify
function f() {
alert(this.name);
alert(this.jméno);
}

f = f.bind( {name: "John"} ).bind( {name: "Ann" } );
f = f.bind( {jméno: "Jan"} ).bind( {jméno: "Anna" } );

f();
```
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
The answer: `undefined`.
Odpověď: `undefined`.

The result of`bind`is another object. It does not have the`test` property.
Výsledkem funkce`bind`je jiný objekt, který nemá vlastnost`test`.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,22 +2,22 @@ importance: 5

---

#Function property after bind
#Vlastnost funkce po bind

There's a value in the property of a function. Will it change after`bind`?Why, or why not?
Ve vlastnosti funkce je nějaká hodnota. Změní se po`bind`?Proč, nebo proč ne?

```js run
functionsayHi() {
alert( this.name );
functionřekniAhoj() {
alert( this.jméno );
}
sayHi.test = 5;
řekniAhoj.test = 5;

*!*
letbound =sayHi.bind({
name: "John"
letvázaná =řekniAhoj.bind({
jméno: "Jan"
});

alert(bound.test ); //what will be the output? why?
alert(vázaná.test ); //jaký bude výstup? A proč?
*/!*
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@

The error occurs because `askPassword` gets functions `loginOk/loginFail` without the object.
Chyba nastane proto, že funkce `zeptejSeNaHeslo` obdrží funkce `přihlášeníOK/přihlášeníSelhalo` bez objektu.

When it calls them, they naturally assume `this=undefined`.
Když je zavolá, přirozeně předpokládají `this=undefined`.

Let's`bind` the context:
Navažme kontext funkcí`bind`:

```js run
functionaskPassword(ok,fail) {
letpassword = prompt("Password?", '');
if (password == "rockstar") ok();
elsefail();
functionzeptejSeNaHeslo(ok,selhal) {
letheslo = prompt("Heslo?", '');
if (heslo == "rockstar") ok();
elseselhal();
}

letuser = {
name: 'John',
letuživatel = {
jméno: 'Jan',

loginOk() {
alert(`${this.name} logged in`);
přihlášeníOK() {
alert(`${this.jméno} se přihlásil`);
},

loginFail() {
alert(`${this.name} failed to log in`);
přihlášeníSelhalo() {
alert(`${this.jméno} se nedokázal přihlásit`);
},

};

*!*
askPassword(user.loginOk.bind(user),user.loginFail.bind(user));
zeptejSeNaHeslo(uživatel.přihlášeníOK.bind(uživatel),uživatel.přihlášeníSelhalo.bind(uživatel));
*/!*
```

Now it works.
Teď to funguje.

An alternative solution could be:
Alternativní řešení by mohlo být:
```js
//...
askPassword(() =>user.loginOk(), () =>user.loginFail());
zeptejSeNaHeslo(() =>uživatel.přihlášeníOK(), () =>uživatel.přihlášeníSelhalo());
```

Usually that also works and looks good.
Obvykle to také funguje a vypadá dobře.

It's a bit less reliable though in more complex situations where `user` variable might change *after* `askPassword` is called, but *before* the visitor answers and calls`() =>user.loginOk()`.
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,35 +2,35 @@ importance: 5

---

#Fix a function that loses "this"
#Opravte funkci, která ztrácí „this

The call to `askPassword()`in the code below should check the password and then call `user.loginOk/loginFail` depending on the answer.
Volání `zeptejSeNaHeslo()`v následujícím kódu by mělo zkontrolovat heslo a pak podle toho, jaká je odpověď, zavolat `uživatel.přihlášeníOK/přihlášeníSelhalo`.

But it leads to an error. Why?
Vede však k chybě. Proč?

Fix the highlighted line for everything to start working right (other lines are not to be changed).
Opravte zvýrazněný řádek tak, aby všechno začalo fungovat správně (ostatní řádky neměňte).

```js run
functionaskPassword(ok,fail) {
letpassword = prompt("Password?", '');
if (password == "rockstar") ok();
elsefail();
functionzeptejSeNaHeslo(ok,selhal) {
letheslo = prompt("Heslo?", '');
if (heslo == "rockstar") ok();
elseselhal();
}

letuser = {
name: 'John',
letuživatel = {
jméno: 'Jan',

loginOk() {
alert(`${this.name} logged in`);
přihlášeníOK() {
alert(`${this.jméno} přihlášen`);
},

loginFail() {
alert(`${this.name} failed to log in`);
přihlášeníSelhalo() {
alert(`${this.jméno} se nedokázal přihlásit`);
},

};

*!*
askPassword(user.loginOk, user.loginFail);
zeptejSeNaHeslo(uživatel.přihlášeníOK, uživatel.přihlášeníSelhalo);
*/!*
```
12 changes: 5 additions & 7 deletions1-js/06-advanced-functions/10-bind/6-ask-partial/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@


1. Either use a wrapper function, an arrow to be concise:
1. Buď použijeme obalovou funkci, pro stručnost šipkovou:

```js
askPassword(() =>user.login(true), () =>user.login(false));
zeptejSeNaHeslo(() =>uživatel.přihlaš(true), () =>uživatel.přihlaš(false));
```

Now it gets `user` from outer variables and runs it the normal way.
Nyní načte proměnnou `uživatel` z vnějších proměnných a funguje běžným způsobem.

2.Or create a partial function from `user.login` that uses `user` as the context and has the correct first argument:
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
askPassword(user.login.bind(user, true),user.login.bind(user, false));
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,33 +2,33 @@ importance: 5

---

#Partial application for login
#Částečná aplikace pro přihlášení

The task is a little more complex variant of <info:task/question-use-bind>.
Tato úloha je trochu složitější variantou úlohy <info:task/question-use-bind>.

The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
Objekt `uživatel` se změnil. Nyní má místo dvou funkcí `přihlášeníOK/přihlášeníSelhalo` jedinou funkci `uživatel.přihlaš(true/false)`.

What should we pass `askPassword` in the code below, so that it calls `user.login(true)`as `ok`and `user.login(false)`as `fail`?
Co bychom měli předat funkci `zeptejSeNaHeslo` v následujícím kódu, aby volala `uživatel.přihlaš(true)`jako `ok`a `uživatel.přihlaš(false)`jako `selhal`?

```js
functionaskPassword(ok,fail) {
letpassword = prompt("Password?", '');
if (password == "rockstar") ok();
elsefail();
functionzeptejSeNaHeslo(ok,selhal) {
letheslo = prompt("Heslo?", '');
if (heslo == "rockstar") ok();
elseselhal();
}

letuser = {
name: 'John',
letuživatel = {
jméno: 'Jan',

login(result) {
alert( this.name + (result ? 'logged in' : 'failed to log in') );
přihlaš(výsledek) {
alert( this.jméno + (výsledek ? 'se přihlásil' : 'se nedokázal přihlásit') );
}
};

*!*
askPassword(?, ?); // ?
zeptejSeNaHeslo(?, ?); // ?
*/!*
```

Your changes should only modify the highlighted fragment.
Vaše změny by měly modifikovat pouze zvýrazněnou část.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp