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

Commit 20c592f

Browse files
committed
closes #1387
1 parent 0280d51 commit 20c592f

File tree

1 file changed

+24
-0
lines changed
  • 1-js/08-prototypes/03-native-prototypes/2-defer-to-prototype-extended

1 file changed

+24
-0
lines changed

‎1-js/08-prototypes/03-native-prototypes/2-defer-to-prototype-extended/solution.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@ function f(a, b) {
1515

1616
f.defer(1000)(1, 2); // shows 3 after 1 sec
1717
```
18+
19+
Please note: we use `this` in `f.apply` to make our decoration work for object methods.
20+
21+
So if the wrapper function is called as an object method, then `this` is passed to the original method `f`.
22+
23+
```js run
24+
Function.prototype.defer = function(ms) {
25+
let f = this;
26+
return function(...args) {
27+
setTimeout(() => f.apply(this, args), ms);
28+
}
29+
};
30+
31+
let user = {
32+
name: "John",
33+
sayHi() {
34+
alert(this.name);
35+
}
36+
}
37+
38+
user.sayHi = user.sayHi.defer(1000);
39+
40+
user.sayHi();
41+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /