Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Created February 11, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Experiment5X/8eee05ef311ef08bc2e6f40d16b8b71f to your computer and use it in GitHub Desktop.
Save Experiment5X/8eee05ef311ef08bc2e6f40d16b8b71f to your computer and use it in GitHub Desktop.
Javascript prototypes
/**
* Class definition for Monster
*/
function Monster(name) {
this.health = 1000;
this.attack = 100;
this.name = name;
}
Monster.prototype.attack = function(other) {
other.health -= this.attack;
}
Monster.prototype.special_attack = function(other) {
if (other.health <= 0) {
other.health = 0;
console.log(this.name + ' died');
}
}
/* End class definition */
/**
* Class definition for Wizard
*/
function Wizard(name) {
// call the parent constructor
Monster.call(this, name);
this.magic = 300;
this.spell_attack = 150;
}
Wizard.prototype = Object.create(Monster.prototype);
// overrides the parent function
Wizard.prototype.special_attack = function(other) {
if (this.magic >= this.spell_attack) {
other.health -= this.spell_attack;
this.magic -= this.spell_attack;
console.log('Cast spell');
} else {
console.log('Not enough magic to cast a spell');
}
// call the parent function
Object.getPrototypeOf(Human.prototype).special_attack.call(this, other);
}
/* End class definition */
/**
* Class definition for Human
*/
function Human(name) {
// call the parent constructor
Monster.call(this, name);
this.arrow_count = 3;
this.arrow_attack = 125;
}
Human.prototype = Object.create(Monster.prototype);
// overrides the parent function
Human.prototype.special_attack = function(other) {
if (this.arrow_count > 0) {
other.health -= this.arrow_attack;
this.arrow_count--;
console.log('Shot arrow')
} else {
console.log('No more arrows')
}
// call the parent function
Object.getPrototypeOf(Human.prototype).special_attack.call(this, other);
}
/* End class definition */
var wizard = new Wizard('Merlin');
var human = new Human('John Cena');
wizard.special_attack(human);
for (var i = 0; i < 12; i++) {
human.special_attack(wizard);
human.arrow_count++;
}
// this is the constructor of the Person object
function Person(name, age, email) {
// instance variables
this.name = name || "";
this.age = age || 0;
this.email = email || "";
}
// a class method (aka functions as static variables)
Person.prototype.print = function() {
console.log(this.name += " is " + this.age.toString() + " years old.");
};
Person.prototype.canBuyAlcohol = function() {
return this.age >= this.DRINKING_AGE;
}
// static variable
Person.prototype.DRINKING_AGE = 21;
var adam = new Person('Adam', 22, 'adam@gmail.com');
var billy = new Person('Billy', 19, 'billy@mail.co.ca');
// overriding the drinking age for only billy, the interpreter first checks the instance variables and then
// checks the variables in the prototype so it will go with this instance value
billy.DRINKING_AGE = 19;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment