_createBind.js 853 B

12345678910111213141516171819202122232425262728
  1. var createCtor = require('./_createCtor'),
  2. root = require('./_root');
  3. /** Used to compose bitmasks for function metadata. */
  4. var WRAP_BIND_FLAG = 1;
  5. /**
  6. * Creates a function that wraps `func` to invoke it with the optional `this`
  7. * binding of `thisArg`.
  8. *
  9. * @private
  10. * @param {Function} func The function to wrap.
  11. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  12. * @param {*} [thisArg] The `this` binding of `func`.
  13. * @returns {Function} Returns the new wrapped function.
  14. */
  15. function createBind(func, bitmask, thisArg) {
  16. var isBind = bitmask & WRAP_BIND_FLAG,
  17. Ctor = createCtor(func);
  18. function wrapper() {
  19. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  20. return fn.apply(isBind ? thisArg : this, arguments);
  21. }
  22. return wrapper;
  23. }
  24. module.exports = createBind;