You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
983 B

7 years ago
  1. # bind
  2. Function binding utility.
  3. ## Installation
  4. ```
  5. $ component install component/bind
  6. ```
  7. ## API
  8. - [bind(obj, fn)](#bindobj-fn)
  9. - [bind(obj, fn, ...)](#bindobj-fn-)
  10. - [bind(obj, name)](#bindobj-name)
  11. <a name=""></a>
  12. <a name="bindobj-fn"></a>
  13. ### bind(obj, fn)
  14. should bind the function to the given object.
  15. ```js
  16. var tobi = { name: 'tobi' };
  17. function name() {
  18. return this.name;
  19. }
  20. var fn = bind(tobi, name);
  21. fn().should.equal('tobi');
  22. ```
  23. <a name="bindobj-fn-"></a>
  24. ### bind(obj, fn, ...)
  25. should curry the remaining arguments.
  26. ```js
  27. function add(a, b) {
  28. return a + b;
  29. }
  30. bind(null, add)(1, 2).should.equal(3);
  31. bind(null, add, 1)(2).should.equal(3);
  32. bind(null, add, 1, 2)().should.equal(3);
  33. ```
  34. <a name="bindobj-name"></a>
  35. ### bind(obj, name)
  36. should bind the method of the given name.
  37. ```js
  38. var tobi = { name: 'tobi' };
  39. tobi.getName = function() {
  40. return this.name;
  41. };
  42. var fn = bind(tobi, 'getName');
  43. fn().should.equal('tobi');
  44. ```
  45. ## License
  46. MIT