Daniel
Daniel

Reputation: 35684

YUI3 calling a function

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);

Upvotes: 1

Views: 390

Answers (2)

Luke
Luke

Reputation: 2581

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

or

top.on('click', Y.bind(anim.run, anim));

Upvotes: 4

Eli Grey
Eli Grey

Reputation: 35830

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;

Upvotes: 3

Related Questions