这个关键字似乎没有像它应该的那样工作

时间:2013-04-19 20:11:36

标签: javascript canvas this keyword

!CLOSED!

嘿,我一直在乱用JavaScript中的对象,我遇到了一个问题:

function Game(name, canvas, width, height, fps)
{
    this.name = name;
    this.canvas = document.getElementById(canvas);
    this.ctx = this.canvas.getContext('2d');
    this.width = width;
    this.height = height;
    this.fps = fps;

}

Game.prototype.gameloop = function()
{
    //Clear Screen
    this.canvas.width = this.canvas.width;

    //Update
    //Update();
    //Draw
    Draw(this.ctx);//this is now pointing at 'Game'
};

Game.prototype.start = function() { 
    this.interval = setInterval(this.gameloop.bind(this),this.fps); 
};

我无法弄清楚为什么在这种情况下'this'关键字不指向'Game'

有什么建议吗?

0 个答案:

没有答案