我有一个看起来像这样的应用程序对象构造函数:
var app = function(loadedsettings) {
return {
init: function() {
this.loop();
},
loop: function() {
this.update();
window.requestAnimationFrame(this.loop);
},
update: function() {
//loop through settings and call update on every object.
},
settings: [
//array of settings objects, all with update methods.
]
};
}
然后当我这样做时:
var theApp = app(settings);
theApp.init();
我得到:
Uncaught TypeError: Object [object global] has no method 'update'
因为在调用 requestAnimationFrame 时,循环函数内部的 this-value 被设置为 window。
有人知道如何调用 requestAnimatinFrame 并将“theApp”对象设置为 this 值吗?