- describes how an object should be created.
- simler objects
- each created object is known as an instance of that object...
This is an example of a Constructor Function.
function Monster(rank) { // the "M" Monster tells other its a constructor Function.
this.rank = rank; // explains what the function name or rank is
this.health = 100; // starting heath is 100
this.takeHit = function() { // a method that takes damage to the heath
this.health--;
}
this.isDead = function() { // when he dies return the health.
return this.health <= 0;
}
}
var monster = new Monster("Captain"); // a new instance of the Constructor
var badGuy = new Monster("bigfoot");
// a new instance of the ConstructorAnother:
function Dice(sides) {
this.sides = sides,
this.roll = function () {
var randomNumber = Math.floor(Math.random() * this.sides) + 1;
return randomNumber;
}
}
No comments:
Post a Comment