createBindWrapper.js 622 B

12345678910111213141516171819202122
  1. var createCtorWrapper = require('./createCtorWrapper');
  2. /**
  3. * Creates a function that wraps `func` and invokes it with the `this`
  4. * binding of `thisArg`.
  5. *
  6. * @private
  7. * @param {Function} func The function to bind.
  8. * @param {*} [thisArg] The `this` binding of `func`.
  9. * @returns {Function} Returns the new bound function.
  10. */
  11. function createBindWrapper(func, thisArg) {
  12. var Ctor = createCtorWrapper(func);
  13. function wrapper() {
  14. var fn = (this && this !== global && this instanceof wrapper) ? Ctor : func;
  15. return fn.apply(thisArg, arguments);
  16. }
  17. return wrapper;
  18. }
  19. module.exports = createBindWrapper;