Dan*_*nny 4 javascript methods class
我在一个文件中有以下代码,但它似乎不起作用.
我基本上试图创建一个对象,并尝试简单地调用对象的功能并显示它,它不会这样做,我不知道为什么.
var mice = new Mice(10, 10);
function Mice(posX, posY)
{
this.x = posX;
this.y = posY;
this.moveLeft = function ()
{
this.x = x - 1;
}
this.moveRight = function ()
{
this.x = x + 1;
}
this.getXPos = function ()
{
return this.x;
}
}
document.onkeydown = function(e)
{
//document.getElementById("mainBody").innerHTML = e.keyCode;
switch(e.keyCode)
{
case 37:
//document.getElementById("mainBody").innerHTML = "you have pressed left";
mice.moveLeft();
document.getElementById("mainBody").innerHTML = mice.getXPos();
break;
default:
//do nothing
break;
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助尝试使这项工作将不胜感激.
谢谢
在"移动"功能中,您必须始终如一地参考this.x:
this.x = this.x - 1;
Run Code Online (Sandbox Code Playgroud)
同样,"getXPos"函数还必须:
return this.x;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |