Guest User

Untitled

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. /**
  2. * Allows partial application of a function.
  3. */
  4. Function.prototype.partial = function () {
  5. var fn = this, args = Array.prototype.slice.call(arguments);
  6. return function () {
  7. return fn.apply(this, args.concat(
  8. Array.prototype.slice.call(arguments)));
  9. };
  10. };
  11.  
  12.  
  13. //examples:
  14.  
  15. var add = function(x, y) { return x + y; };
  16.  
  17.  
  18. var add2 = add.partial(2);
  19. console.log(add2(2)); // shows 4
  20.  
  21. var add2and2 = add.partial(2,2);
  22. console.log(add2and2()); //shows 4
Add Comment
Please, Sign In to add comment