我有一个看起来像这样的对象链:
Game.world.update()
我想使用 requestAnimationFrame 来确定该函数的帧速率。
但是,当我这样实现时:
World.prototype.update = function()
{
requestAnimationFrame(this.update);
}
范围从世界对象变为窗口对象。在调用 requestAnimationFrame() 时如何保持我想要的范围?我知道这与匿名函数等有关,但我无法理解它。
通常的方法,在任何地方都适用:
World.prototype.update = function()
{
var self = this;
requestAnimationFrame(function(){self.update()});
}
或者使用 ES5Function.prototype.bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind (兼容性 http://kangax.github.io/es5-compat-table/#Function.prototype.bind%5d):
World.prototype.update = function()
{
requestAnimationFrame(this.update.bind(this)});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)