bindAll.js 639 B

12345678910111213141516171819
  1. var restArguments = require('./restArguments.js');
  2. var _flatten = require('./_flatten.js');
  3. var bind = require('./bind.js');
  4. // Bind a number of an object's methods to that object. Remaining arguments
  5. // are the method names to be bound. Useful for ensuring that all callbacks
  6. // defined on an object belong to it.
  7. var bindAll = restArguments(function(obj, keys) {
  8. keys = _flatten(keys, false, false);
  9. var index = keys.length;
  10. if (index < 1) throw new Error('bindAll must be passed function names');
  11. while (index--) {
  12. var key = keys[index];
  13. obj[key] = bind(obj[key], obj);
  14. }
  15. return obj;
  16. });
  17. module.exports = bindAll;