Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Converts any function to accept continuation-passing style
- //
- // var add = function(a, b) {
- // return a + b;
- // };
- //
- // var addcps = add.toCPS();
- //
- // addcps(3, 4, function(x) { alert(x) });
- Function.prototype.toCPS = function() {
- var direct = this;
- return function() {
- var args = [], i = arguments.length,
- cps, context, result;
- while (i--) args[i] = arguments[i]; // (1)
- context = (args[args.length-2] instanceof Function)
- ? args.pop() : null; // (2)
- cps = args.pop();
- result = direct.apply(this, args); // (3)
- cps.call(context, result);
- };
- };
Add Comment
Please, Sign In to add comment