2017-08-29 31 views
1

我有一个将传递给requestAnimationFrame的方法的对象。 目前我创建的对象,而不是使用箭头函数重新分配方法,它返回。使用箭头函数作为传递给requestAnimationFrame的方法

var player={fn:function(){return()=>{game.circle(this.x,this.y,this.radius)}}, 
x:200,y:300,radius:20}; 
player.fn=player.fn(); 

这样做可以在创建对象后不重新分配方法吗?

回答

2

你可以只use a static reference to player instead

const player = { 
    fn() { 
    game.circle(player.x, player.y, player.radius); 
    }, 
    x:200, 
    y:300, 
    radius:20 
}; 
requestAnimationFrame(player.fn); 

但是,没有,否则就没有办法写,而不需要单独分配。通常你会然而刚刚bind or use the arrow function打电话时​​:

var player = { 
    fn() { 
    game.circle(this.x, this.y, this.radius); 
    }, 
    x:200, 
    y:300, 
    radius:20 
}; 
requestAnimationFrame(() => player.fn()); 
+0

虽然你的答案会工作,我路过的方法来请求动画帧的方式,这是行不通的。然而你的链接绑定应该正常工作谢谢 – user7951676

+0

@ user7951676那么你没有在问题中显示你的代码的一部分,所以我不能建议它 – Bergi

+0

这很好,我明白,谢谢你 – user7951676

相关问题