closes #1387
This commit is contained in:
parent
0280d51f7a
commit
20c592f95e
1 changed files with 24 additions and 0 deletions
|
@ -15,3 +15,27 @@ function f(a, b) {
|
|||
|
||||
f.defer(1000)(1, 2); // shows 3 after 1 sec
|
||||
```
|
||||
|
||||
Please note: we use `this` in `f.apply` to make our decoration work for object methods.
|
||||
|
||||
So if the wrapper function is called as an object method, then `this` is passed to the original method `f`.
|
||||
|
||||
```js run
|
||||
Function.prototype.defer = function(ms) {
|
||||
let f = this;
|
||||
return function(...args) {
|
||||
setTimeout(() => f.apply(this, args), ms);
|
||||
}
|
||||
};
|
||||
|
||||
let user = {
|
||||
name: "John",
|
||||
sayHi() {
|
||||
alert(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
user.sayHi = user.sayHi.defer(1000);
|
||||
|
||||
user.sayHi();
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue