Created
September 27, 2015 21:34
-
-
Save qetr1ck-op/a292315fd0ddd6477827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. add defer() to all function prototypes | |
2. should wait for n second, then invoke a function | |
3. (!) and return a function | |
function f() { | |
console.log( a + b ); | |
} | |
f.defer(1000)(1, 2); //after delay 1sec, 3 | |
*/ | |
Function.prototype.defer = function(ms) { | |
return function() { | |
var args = arguments; | |
setTimeout(function() { | |
this.apply(this, args); | |
}.bind(this), ms); | |
}.bind(this) | |
} | |
/* | |
in ES6 | |
Function.prototype.defer = function(ms) { | |
var self = this; | |
return function() { | |
setTimeout(() => { | |
self.apply(this, arguments); | |
}, ms); | |
} | |
} | |
*/ | |
function f(a, b) { | |
console.log( a + b ); | |
} | |
f.defer(1000)(1, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment