YUI3调用函数

发布于 2024-08-12 18:37:29 字数 157 浏览 2 评论 0原文

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

我有一个动画函数,想知道为什么我不能这样调用它

top.on('click', anim.run);
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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-08-19 18:37:29
top.on('click', function () { anim.run(); });

或者

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

or

top.on('click', Y.bind(anim.run, anim));
冰火雁神 2024-08-19 18:37:29

因为在第二种情况下,this 不是 anim,因为您正在检索 run 函数,而不是从 anim 调用它>。

例如:

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

a.b() === 1;
var bMethod = a.b;
bMethod() === 2;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文