_ctx.js 520 B

123456789101112131415161718192021
  1. // optional / simple context binding
  2. var aFunction = require('./_a-function');
  3. module.exports = function (fn, that, length) {
  4. aFunction(fn);
  5. if (that === undefined) return fn;
  6. switch (length) {
  7. case 1: return function (a) {
  8. return fn.call(that, a);
  9. };
  10. case 2: return function (a, b) {
  11. return fn.call(that, a, b);
  12. };
  13. case 3: return function (a, b, c) {
  14. return fn.call(that, a, b, c);
  15. };
  16. }
  17. return function (/* ...args */) {
  18. return fn.apply(that, arguments);
  19. };
  20. };