用js的面向对象的方式给对象两个属性canvas和ctx,分别表示画布和画布上下文,在draw函数中调用的时候为什么canvas属性返回的是type是object,而ctx属性返回的是undefined,按理说ctx应该也是一个object啊,哪位大侠可以帮忙看看。<!DOCTYPE html>
<html>
<head>
<title>test code</title>
<script type="text/javascript">
function GameObjectManager(){
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
};
GameObjectManager.prototype.draw = function(){
alert(typeof(this.canvas));
alert(typeof(this.ctx));
};
GameObjectManager.prototype.run = function(){
setInterval(this.draw, 60);
};
window.onload = init; function init(){
var gameObjectManager = new GameObjectManager();
gameObjectManager.run();
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>html5javascriptcanvasgetContext
<html>
<head>
<title>test code</title>
<script type="text/javascript">
function GameObjectManager(){
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
};
GameObjectManager.prototype.draw = function(){
alert(typeof(this.canvas));
alert(typeof(this.ctx));
};
GameObjectManager.prototype.run = function(){
setInterval(this.draw, 60);
};
window.onload = init; function init(){
var gameObjectManager = new GameObjectManager();
gameObjectManager.run();
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>html5javascriptcanvasgetContext
var _this = this;
setInterval(function(){
_this.draw();
}, 60);
};