我有一个带有方法的对象,该方法将传递给 requestAnimationFrame。目前,我创建对象而不是使用它返回的箭头函数重新分配方法。
var player={fn:function(){return ()=>{game.circle(this.x,this.y,this.radius)}},
x:200,y:300,radius:20};
player.fn=player.fn();
这可以在创建对象后不重新分配方法的情况下完成吗?
我有一个带有方法的对象,该方法将传递给 requestAnimationFrame。目前,我创建对象而不是使用它返回的箭头函数重新分配方法。
var player={fn:function(){return ()=>{game.circle(this.x,this.y,this.radius)}},
x:200,y:300,radius:20};
player.fn=player.fn();
这可以在创建对象后不重新分配方法的情况下完成吗?
您可以只使用静态引用来player
代替:
const player = {
fn() {
game.circle(player.x, player.y, player.radius);
},
x:200,
y:300,
radius:20
};
requestAnimationFrame(player.fn);
但是不,否则没有单独的分配就没有办法写这个。然而,通常你会在调用时只bind
使用或使用箭头函数requestAnimationFrame
:
var player = {
fn() {
game.circle(this.x, this.y, this.radius);
},
x:200,
y:300,
radius:20
};
requestAnimationFrame(() => player.fn());