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

我有一个动画功能,并想知道为什么不能这样称呼它

top.on('click', anim.run);
有帮助吗?

解决方案

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

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

其他提示

由于作为你检索this功能,而不是从anim调用它run未在第二种情况下anim

例如:

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

a.b() === 1;
var bMethod = a.b;
bMethod() === 2;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top