views:

76

answers:

2
top.on('click', function(){
    anim.run();
});

I have an anim function, and was wondering why I can't call it like this

top.on('click', anim.run);
+2  A: 

Because this is not anim in the second case as you're retrieving the run function and not calling it from anim.

For example:

var a = {
  b: function () {
    return this.c;
  },
  c: 1
},
c = 2;

a.b() === 1;
var bMethod = a.b;
bMethod() === 2;
Eli Grey
ok, so top.on('click', parent.anim.run); worksthanks
Daniel
No, unless `run` doesn't reference `this`.
Eli Grey
+1  A: 
top.on('click', function () { anim.run(); });

or

top.on('click', Y.bind(anim.run, anim));
Luke
Thanks, good to see a YUI team member!
Daniel