OOP样式的Javascript setInterval()不工作
我正在创建一个程序,使用javascript反复移动对象。函数在分开时可以工作,但当我尝试使用OOP模式时,会反复出现一个奇怪的错误OOP样式的Javascript setInterval()不工作,javascript,Javascript,我正在创建一个程序,使用javascript反复移动对象。函数在分开时可以工作,但当我尝试使用OOP模式时,会反复出现一个奇怪的错误 Uncaught TypeError: this.Move is not a function 这是我的密码 function Bot(){ this.XPos =0; this.YPos=0; this.AsyncMove=setInterval(function(){ this.XPos+=10;
Uncaught TypeError: this.Move is not a function
这是我的密码
function Bot(){
this.XPos =0;
this.YPos=0;
this.AsyncMove=setInterval(function(){
this.XPos+=10;
this.YPos+=10;
this.Move();
},100);
}
Bot.prototype = {
constructor:Bot,
Move:function(){
console.log(this.XPos+" ,"+this.YPos);
}
};
您应该像这样将当前实例绑定到匿名函数
this.AsyncMove=setInterval(function(){
this.XPos+=10;
this.YPos+=10;
this.Move();
}.bind(this),100);
可能重复:的可能重复