我正在用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