bindAll.js 602 B

123456789101112131415161718
  1. import restArguments from './restArguments.js';
  2. import flatten from './_flatten.js';
  3. import bind from './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. export default 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. });