I'm writing some Javascript project and have some problems with variable update. I have such a function:
function Bullet(top){
this.top = top;
update();
function update(){
this.top -= 5;
console.log(this.top)
setTimeout(update, 50);
}
};
Bullet.prototype.getTop = function(){
return this.top;
};
When I call:
var bullet = new Bullet(300);
I get a continuous console output of 300. Of course when I call bullet.getTop()
repeatedly, I also get result 300.
Now, my questions are: Why is this happening? How do I fix it?