I just started a tutorial in game development using JavaScript and HTML5. javascript.prototype has come into play many times. I can not really understand how it works. Here is a link I found with a good explanation, but I am still a little bit confused
How does JavaScript .prototype work?.
Can anyone explain this please? Here is an example of my code:
function Enemy(){
this.srcX = 140; //gets the location of enemy in x and Y here
this.srcY = 600;
this.width = 45;//gets the enemies width and height here
this.height = 54;
this.drawX = randomRange(0, canvasWidth - this.width);
this.drawY = randomRange(0, canvasHeight - this.height);
this.centerX = this.drawX + (this.width / 2);
this.centerY = this.drawY +(this.height / 2);
//this.targetX = this.centerX;
//this.targetY = this.centerY;
//this.randomMoveTime = randomRange(4000,10000);
this.speed = 1;
//var that = this;
//this.moveInterval = setInterval(function() {that.setTargetLocation();},that.randomMOveTime);
this.isDead = false;
}
Enemy.prototype.update = function() {
//this.checkDirection();
this.centerX = this.drawX + (this.width / 2);
this.centerY = this.drawY + (this.height / 2);
}
updatefunction".new Enemy()is (roughly) equivalent tovar newObj = Object.create(Enemy.prototype); Enemy.apply(newObj, arguments); return newObj;?Enemy.prototypeis an object.Enemy.prototype.update = function() {...};assigns a function to theupdateproperty of that object. Since the property doesn't exist yet, it is created. Simpler example:var foo = {}; foo.bar = function() { console.log('hi'); }; foo.bar();. This has nothing to do with prototypes. This is how objects in JavaScript work. developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…