bindAll.js 632 B

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