function-bind-context.js 599 B

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