Posted By

dhjapan on 10/04/11


Tagged

javascript object Delegate


Versions (?)

Delegate


 / Published in: JavaScript
 

  1. // used to fix "this" prob with Function.apply to give call proper scope
  2. // nice method to put in your lib
  3. function delegate(instance, method) {
  4. return function() {
  5. return method.apply(instance, arguments);
  6. }
  7. }
  8.  
  9. function Animal(name) {
  10. this.name = name;
  11. this.hello = function() {
  12. alert("hello " + this.name);
  13. }
  14. }
  15.  
  16. var dog = new Animal("Jake");
  17. var button = {
  18. onclick : delegate(dog, dog.hello)
  19. };
  20. button.onclick();

Report this snippet  

You need to login to post a comment.