Skip to content

Commit 8bf36b7

Browse files
committed
ok
1 parent 1c0bebe commit 8bf36b7

File tree

16 files changed

+639
-172
lines changed

16 files changed

+639
-172
lines changed

1-js/7-deeper/5-settimeout-setinterval/5-setinterval-result/solution.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

1-js/7-deeper/5-settimeout-setinterval/5-setinterval-result/task.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

1-js/7-deeper/5-settimeout-setinterval/6-who-runs-faster/solution.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

1-js/7-deeper/5-settimeout-setinterval/6-who-runs-faster/task.md

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The answer: `null`.
2+
3+
4+
```js run
5+
function f() {
6+
alert( this ); // null
7+
}
8+
9+
var user = {
10+
g: f.bind(null)
11+
};
12+
13+
user.g();
14+
```
15+
16+
The context of a bound function is hard-fixed. There's just no way to further change it.
17+
18+
So even while we run `user.g()`, the original function is called with `this=null`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
importance: 5
2+
3+
---
4+
5+
# Bound function as a method
6+
7+
What will be the output?
8+
9+
```js
10+
function f() {
11+
alert( this ); // ?
12+
}
13+
14+
var user = {
15+
g: f.bind(null)
16+
};
17+
18+
user.g();
19+
```
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The answer: **John**.
2+
3+
```js run no-beautify
4+
function f() {
5+
alert(this.name);
6+
}
7+
8+
f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
9+
10+
f(); // John
11+
```
12+
13+
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 the creation time.
14+
15+
A function cannot be re-bound.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
importance: 5
2+
3+
---
4+
5+
# Second bind
6+
7+
Can we change `this` by additional binding?
8+
9+
What will be the output?
10+
11+
```js no-beautify
12+
function f() {
13+
alert(this.name);
14+
}
15+
16+
f = f.bind( {name: "John"} ).bind( {name: "Ann" } );
17+
18+
f();
19+
```
20+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The answer: `undefined`.
2+
3+
The result of `bind` is another object. It does not have the `test` property.
4+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
importance: 5
2+
3+
---
4+
5+
# Function property after bind
6+
7+
There's a value in the property of a function. Will it change after `bind`? Why, elaborate?
8+
9+
```js run
10+
function sayHi() {
11+
alert( this.name );
12+
}
13+
sayHi.test = 5;
14+
15+
*!*
16+
let bound = sayHi.bind({
17+
name: "John"
18+
});
19+
20+
alert( bound.test ); // what will be the output? why?
21+
*/!*
22+
```
23+

0 commit comments

Comments
 (0)