bind.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _arity from "./internal/_arity.js";
  2. import _curry2 from "./internal/_curry2.js";
  3. /**
  4. * Creates a function that is bound to a context.
  5. * Note: `R.bind` does not provide the additional argument-binding capabilities of
  6. * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.6.0
  11. * @category Function
  12. * @category Object
  13. * @sig (* -> *) -> {*} -> (* -> *)
  14. * @param {Function} fn The function to bind to context
  15. * @param {Object} thisObj The context to bind `fn` to
  16. * @return {Function} A function that will execute in the context of `thisObj`.
  17. * @see R.partial
  18. * @example
  19. *
  20. * const log = R.bind(console.log, console);
  21. * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
  22. * // logs {a: 2}
  23. * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
  24. */
  25. var bind =
  26. /*#__PURE__*/
  27. _curry2(function bind(fn, thisObj) {
  28. return _arity(fn.length, function () {
  29. return fn.apply(thisObj, arguments);
  30. });
  31. });
  32. export default bind;