Skip to content

Instantly share code, notes, and snippets.

@mattbrannon
Last active October 18, 2018 22:19
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 mattbrannon/ad1819031d8e59d93e6333d7ab2196c1 to your computer and use it in GitHub Desktop.
Save mattbrannon/ad1819031d8e59d93e6333d7ab2196c1 to your computer and use it in GitHub Desktop.
Functional class, Prototypal class, Pseudoclassical class
//////////////////////
// Functional class //
//////////////////////
const Dog = function(name, breed, age){
var dog = {
name: name,
breed: breed,
age: age,
happiness: 50,
hunger: 10,
energy: 100
};
dog.feed = function(food){
if (dog.hunger - food > 0) {
dog.hunger -= food;
} else {
dog.hunger = 0;
}
};
dog.play = function(time){
if (dog.happiness + time > 100){
dog.happiness += time;
} else {
dog.happiness = 100;
}
};
dog.nap = function(time){
if (dog.energy + time < 100){
dog.energy += time;
} else {
dog.energy = 100;
}
}
return dog;
}
//////////////////////
// Prototypal class //
//////////////////////
const Dog = function(name, breed, age){
var dog = Object.create(Dog.prototype);
dog.name = name;
dog.breed = breed;
dog.age = age;
dog.happiness = 50;
dog.hunger = 10;
dog.energy = 100;
return dog;
}
Dog.prototype.feed = function(food){
if (this.hunger - food > 0) {
this.hunger -= food;
} else {
this.hunger = 0;
}
};
Dog.prototype.play = function(time){
if (this.happiness + time > 100){
this.happiness += time;
} else {
this.happiness = 100;
}
};
Dog.prototype.nap = function(time){
if (this.energy + time < 100){
this.energy += time;
} else {
this.energy = 100;
}
}
///////////////////////////
// Pseudoclassical class //
///////////////////////////
const Dog = function(name, breed, age){
this.name = name;
this.breed = breed;
this.age = age;
this.happiness = 50;
this.hunger = 10;
this.energy = 100;
}
Dog.prototype.feed = function(food){
if (this.hunger - food > 0) {
this.hunger -= food;
} else {
this.hunger = 0;
}
};
Dog.prototype.play = function(time){
if (this.happiness + time > 100){
this.happiness += time;
} else {
this.happiness = 100;
}
if (this.energy - time > 0){
this.energy -= time;
} else {
this.energy = 0;
}
};
Dog.prototype.nap = function(time){
if (this.energy + time < 100){
this.energy += time;
} else {
this.energy = 100;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment