我正在用Javascript構建一個簡單的遊戲引擎,並且我碰到了一個名爲「所有函數都是對象類」的牆。關於Javascript中變量可見性的困惑
更具體地說,我有類
function Game(){
}
Game.prototype = {
update: function(){/* Blah */},
draw: function(){/* Foo */},
start: function(fps){
//Just a global var where I keep the FPS
GS.fps = fps;
var start = new Date().getTime();
var time = 0;
function timer(){
time += (1000/fps);
var diff = (new Date().getTime() - start) - time;
if(true)
{
//Execute on itteration of the game loop
this.update(); //It breaks here because this. is timer, not G.Game
//Draw everything
this.draw();
window.setTimeout(timer,(1000/GS.fps - diff));
}
};
}
我想用全局對象作爲更新容器和借鑑作用,但這只是覺得我錯了...是否有任何其他方式?有沒有一種原生的JS訪問父類的方式?
謝謝你的時間!
'如果(真)'.... – jAndy 2012-02-06 22:06:17
我敢肯定這無關我的js OOP ... – Loupax 2012-02-06 22:08:28