fixes #1563
This commit is contained in:
parent
a967ad0cf9
commit
ad08b46c54
1 changed files with 12 additions and 3 deletions
|
@ -83,10 +83,12 @@ let user = {
|
|||
|
||||
setTimeout(() => user.sayHi(), 1000);
|
||||
|
||||
// ...within 1 second
|
||||
user = { sayHi() { alert("Another user in setTimeout!"); } };
|
||||
// ...the value of user changes within 1 second
|
||||
user = {
|
||||
sayHi() { alert("Another user in setTimeout!"); }
|
||||
};
|
||||
|
||||
// Another user in setTimeout?!?
|
||||
// Another user in setTimeout!
|
||||
```
|
||||
|
||||
The next solution guarantees that such thing won't happen.
|
||||
|
@ -159,9 +161,16 @@ let user = {
|
|||
let sayHi = user.sayHi.bind(user); // (*)
|
||||
*/!*
|
||||
|
||||
// can run it without an object
|
||||
sayHi(); // Hello, John!
|
||||
|
||||
setTimeout(sayHi, 1000); // Hello, John!
|
||||
|
||||
// even if the value of user changes within 1 second
|
||||
// sayHi uses the pre-bound value
|
||||
user = {
|
||||
sayHi() { alert("Another user in setTimeout!"); }
|
||||
};
|
||||
```
|
||||
|
||||
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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue