Skip to content

Commit ad08b46

Browse files
committed
fixes #1563
1 parent a967ad0 commit ad08b46

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

1-js/06-advanced-functions/10-bind/article.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ let user = {
8383

8484
setTimeout(() => user.sayHi(), 1000);
8585

86-
// ...within 1 second
87-
user = { sayHi() { alert("Another user in setTimeout!"); } };
86+
// ...the value of user changes within 1 second
87+
user = {
88+
sayHi() { alert("Another user in setTimeout!"); }
89+
};
8890

89-
// Another user in setTimeout?!?
91+
// Another user in setTimeout!
9092
```
9193

9294
The next solution guarantees that such thing won't happen.
@@ -159,9 +161,16 @@ let user = {
159161
let sayHi = user.sayHi.bind(user); // (*)
160162
*/!*
161163

164+
// can run it without an object
162165
sayHi(); // Hello, John!
163166

164167
setTimeout(sayHi, 1000); // Hello, John!
168+
169+
// even if the value of user changes within 1 second
170+
// sayHi uses the pre-bound value
171+
user = {
172+
sayHi() { alert("Another user in setTimeout!"); }
173+
};
165174
```
166175

167176
In the line `(*)` we take the method `user.sayHi` and bind it to `user`. The `sayHi` is a "bound" function, that can be called alone or passed to `setTimeout` -- doesn't matter, the context will be right.

0 commit comments

Comments
 (0)