使用此关键字的 requestAnimationFrame
我正在使用 webkitRequestAnimationFrame
但在对象内部使用它时遇到问题。如果我传递 this
关键字,它将使用 window
并且我找不到让它使用指定对象的方法。
示例:
Display.prototype.draw = function(){
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
//Animation stuff here.
window.webkitRequestAnimationFrame(this.draw);
};
我也尝试过,但没有成功:
Display.prototype.draw = function(){
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
//Animation stuff here.
var draw = this.draw;
window.webkitRequestAnimationFrame(draw);
};
I'm using webkitRequestAnimationFrame
but I'm having trouble using it inside of an object. If I pass the this
keyword it will use window
and I can't find a way for it to use the specified object instead.
Example:
Display.prototype.draw = function(){
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
//Animation stuff here.
window.webkitRequestAnimationFrame(this.draw);
};
I have also tried this but to no avail:
Display.prototype.draw = function(){
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
//Animation stuff here.
var draw = this.draw;
window.webkitRequestAnimationFrame(draw);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
webkitRequestAnimationFrame
可能会调用您传入的函数,如下所示:此时,您已将
draw
函数与其调用上下文分离(分离)。您需要将函数 (draw
) 绑定到其上下文(Display
的实例)。您可以使用
Function.bind
,但这需要 JavaScript 1.8 支持(或者只使用推荐的补丁)。webkitRequestAnimationFrame
will presumably call the function you pass in, something like this:At this point, you have dissociated (detached) the
draw
function from its invocation context. You need to bind the function (draw
) to its context (the instance ofDisplay
).You can use
Function.bind
, but this requires JavaScript 1.8 support (or just use the recommended patch).现在 ES6/2015 已经到来,如果您使用的是转译器,那么箭头函数具有词法
this
绑定,因此您可以执行以下操作而不是::
这更干净一点。
我已经将 Typescript 转换为 ES5 来有效地使用它。
Now that ES6/2015 is here, if you are using a transpiler then an arrow function has lexical
this
binding so instead of:you can do:
which is a bit cleaner.
I've used this effectively with Typescript transpiling to ES5.
我不能保证这是一个好主意并且我是对的,但是在每个 requestAnimationFrame 上运行 .bind 意味着在每次迭代上创建一个新函数。这对我来说听起来不太合适。
这就是为什么在我的项目中我缓存了绑定函数以避免反模式。
简单的例子:
如果您有一个具有原型继承的更复杂的项目,您仍然可以创建一个在对象的构造函数中绑定“this”的缓存函数
。
http://jsfiddle.net/3t9pboe8/ (在控制台中查看)
I can't guarantee that this is a good idea and that I'm right, but running .bind on every requestAnimationFrame means creating a new function on every iteration. It just doesn't sound right to me.
That's why in my project I cached the bound function to avoid the anti-pattern.
Simple example:
If you have a more complex project with prototype inheritance you can still create a cached function with "this" bound in object's constructor
Thoughts?
http://jsfiddle.net/3t9pboe8/ (look in the console)
怎么样:
...假设你使用 jquery
how about this:
...assuming you use jquery
除了
bind
方法和箭头函数解决方案(由 Jamaes World 的答案提供)之外,另一个(相当旧的)解决方法可能是:Beside
bind
method, and arrow function solution (offered by Jamaes World's answer), Another (rather old) work around could be:你不必使用“这个”。
保持简单。
you have not to use "this".
Keep it simple.
而且您可能想使用 requestAnimationFrame shim 使其在所有浏览器上运行 https://github.com/kof /动画框架
And also you might want to use requestAnimationFrame shim to make it work on all browsers https://github.com/kof/animation-frame