Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `null`.
Svaret er: `null`.


```js run
Expand All @@ -13,6 +13,6 @@ let user = {
user.g();
```

The context of a bound function is hard-fixed. There's just no way to further change it.
Konteksten for en bundet funktion er fikseret. Der er ingen måde at ændre den yderligere.

So even while we run `user.g()`, the original function is called with `this=null`.
Så selv når vi kører `user.g()`, kaldes den originale funktion med `this=null`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Bound function as a method
# Bundet funktion som en metode

What will be the output?
Hvad vil output'et være?

```js
function f() {
Expand Down
6 changes: 3 additions & 3 deletions 1-js/06-advanced-functions/10-bind/3-second-bind/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: **John**.
Svaret er: **John**.

```js run no-beautify
function f() {
Expand All @@ -10,6 +10,6 @@ f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
f(); // John
```

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.
Det [eksotiske bundne](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) objekt der returneres af `f.bind(...)` husker den kontekst (og eventuelle argumenter) den fik da den blev oprettet.

A function cannot be re-bound.
En sådan funktion kan ikke blive bundet igen.
6 changes: 3 additions & 3 deletions 1-js/06-advanced-functions/10-bind/3-second-bind/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Second bind
# Anden binding

Can we change `this` by additional binding?
Kan vi ændre `this` ved yderligere binding?

What will be the output?
Hvad vil output'et være?

```js no-beautify
function f() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `undefined`.
Svaret er: `undefined`.

The result of `bind` is another object. It does not have the `test` property.
Resultatet af `bind` er et andet objekt. Det har ikke egenskaben `test`.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Function property after bind
# Funktionsegenskab efter binding

There's a value in the property of a function. Will it change after `bind`? Why, or why not?
Der er en værdi i en egenskab af en funktion. Vil den ændre sig efter `bind`? Hvorfor, eller hvorfor ikke?

```js run
function sayHi() {
Expand All @@ -17,7 +17,7 @@ let bound = sayHi.bind({
name: "John"
});

alert( bound.test ); // what will be the output? why?
alert( bound.test ); // hvad vil output'et være? hvorfor?
*/!*
```

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

The error occurs because `askPassword` gets functions `loginOk/loginFail` without the object.
Fejlen sker fordi `askPassword` får funktionerne `loginOk/loginFail` uden objektet.

When it calls them, they naturally assume `this=undefined`.
Når den kalder dem, antager de `this=undefined`.

Let's `bind` the context:
Lad os `binde` konteksten:

```js run
function askPassword(ok, fail) {
Expand All @@ -16,11 +16,11 @@ let user = {
name: 'John',

loginOk() {
alert(`${this.name} logged in`);
alert(`${this.name} logget ind`);
},

loginFail() {
alert(`${this.name} failed to log in`);
alert(`${this.name} fejl i log in`);
},

};
Expand All @@ -30,14 +30,14 @@ askPassword(user.loginOk.bind(user), user.loginFail.bind(user));
*/!*
```

Now it works.
Nu virker det.

An alternative solution could be:
En alternativ løsning kan være at bruge en wrapper-funktion:
```js
//...
askPassword(() => user.loginOk(), () => user.loginFail());
```

Usually that also works and looks good.
Det virker normalt også fint og ser ok ud.

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()`.
Det er lidt mindre pålideligt i mere komplekse situationer hvor `user` variablen kan ændre sig *efter* `askPassword` er kaldt, men *før* besøgende har svaret og kalder `() => user.loginOk()`.
12 changes: 6 additions & 6 deletions 1-js/06-advanced-functions/10-bind/5-question-use-bind/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Fix a function that loses "this"
# Fix en funktion der mister "this"

The call to `askPassword()` in the code below should check the password and then call `user.loginOk/loginFail` depending on the answer.
Kaldet til `askPassword()` i koden nedenfor skal tjekke passwordet og derefter kalde `user.loginOk/loginFail` afhængigt af svaret.

But it leads to an error. Why?
Men det fører til en fejl. Hvorfor?

Fix the highlighted line for everything to start working right (other lines are not to be changed).
Fix den fremhævede linje for at alt skal virke korrekt (andre linjer skal ikke ændres).

```js run
function askPassword(ok, fail) {
Expand All @@ -21,11 +21,11 @@ let user = {
name: 'John',

loginOk() {
alert(`${this.name} logged in`);
alert(`${this.name} logget ind`);
},

loginFail() {
alert(`${this.name} failed to log in`);
alert(`${this.name} fejl i log in`);
},

};
Expand Down
6 changes: 3 additions & 3 deletions 1-js/06-advanced-functions/10-bind/6-ask-partial/solution.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@


1. Either use a wrapper function, an arrow to be concise:
1. Brug enten en wrapper funktion, en arrow for at gøre det helt kort:

```js
askPassword(() => user.login(true), () => user.login(false));
```

Now it gets `user` from outer variables and runs it the normal way.
Nu henter `user` fra ydre variable og kører normalt.

2. Or create a partial function from `user.login` that uses `user` as the context and has the correct first argument:
2. eller opret en delvis funktion fra `user.login` der bruger `user` som kontekst og det korrekte første argument:


```js
Expand Down
8 changes: 4 additions & 4 deletions 1-js/06-advanced-functions/10-bind/6-ask-partial/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ importance: 5

# Partial application for login

The task is a little more complex variant of <info:task/question-use-bind>.
Denne opgave er en lidt mere kompleks variant af <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)`.
`user` objektet er ændret. Nu har den, i stedet for to funktioner `loginOk/loginFail`, én enkelt funktion `user.login(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`?
Hvad skal vi sende til `askPassword` i koden nedenfor, så den kalder `user.login(true)` som `ok` og `user.login(false)` som `fail`?

```js
function askPassword(ok, fail) {
Expand All @@ -30,5 +30,5 @@ askPassword(?, ?); // ?
*/!*
```

Your changes should only modify the highlighted fragment.
Din ændring skal kun ændre den fremhævede linje.

Loading