Skip to content

Instantly share code, notes, and snippets.

@enigmatikme
Last active July 16, 2018 04:15
Show Gist options
  • Save enigmatikme/c6f5aca8e4139bf2ea31c22c8de7a6ae to your computer and use it in GitHub Desktop.
Save enigmatikme/c6f5aca8e4139bf2ea31c22c8de7a6ae to your computer and use it in GitHub Desktop.
Functional Class
// @JOHN DO NOT CONTINUE.
// this is all original code, nothing added.
// sorry you read this far.
// ORIGINAL
const dog = {
name: 'Scout',
breed: ['Husky', 'German Shepherd'],
age: 4,
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;
}
if(dog.energy - time > 0) {
dog.energy -= time;
} else {
dog.energy = 0;
}
}
dog['nap'] = function(time) {
if(dog.energy + time < 100) {
dog.energy += time;
} else {
dog.energy = 100;
}
}
// Prototypal Class
var Dog = function(time, food) {
var obj = Object.create(Dog.prototype);
obj.name = 'Scout';
obj.breed = ['Husky', 'German Shepherd'];
obj.time = time
obj.food = food;
obj.age = 4
obj.happiness = 50;
obj.hunger = 10;
obj.energy = 100;
return obj;
};
Dog.prototype.feed = function (food) {
if(this.hunger - food > 0) {
this.hunger -= food;
} else {
this.hunger = 0;
}
}
Dog.prototype.play = function() {
if(this.happiness + this.time < 100) {
this.happiness += this.time;
} else {
this.happiness = 100;
}
if(this.energy - this.time > 0) {
this.energy -= this.time;
} else {
this.energy = 0;
}
}
Dog.prototype.nap = function() {
if(this.energy + this.time < 100) {
this.energy += obj.time;
} else {
console.log(this.energy);
this.energy = 100;
}
}
var newDog = Dog(100, 50);
console.log(newDog);
// Pseudoclassical Class
var Dog = function(time, food) {
this.name = 'Scout';
this.breed = ['Husky', 'German Shepherd'];
this.time = time;
this.food = food;
this.age = 4;
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() { //how this is similar
if(this.happiness + this.time < 100) {
this.happiness += this.time;
} else {
this.happiness = 100;
}
if(this.energy - this.time > 0) {
this.energy -= this.time;
} else {
this.energy = 0;
}
}
Dog.prototype.nap = function() {
if(this.energy + this.time < 100) {
this.energy += obj.time;
} else {
console.log(this.energy);
this.energy = 100;
}
}
var newDog = new Dog(100, 50);
console.log(newDog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment