Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //// Functional Class (with duplicate methods - I know this can be taken further though)
- const Dog = function (food, time){
- var obj = {
- name: 'Scout',
- breed: ['Husky', 'German Shepherd'],
- age: 4,
- happiness: 50,
- hunger: 10,
- energy: 100,
- }
- obj.feed = function {
- if(dog.hunger - food > 0) {
- dog.hunger -= food;
- } else {
- dog.hunger = 0;
- }
- obj.play = function {
- if(dog.happiness + time < 100) {
- dog.happiness += time;
- } else {
- dog.happiness = 100;
- }
- obj.nap = function {
- if(dog.energy + time < 100) {
- dog.energy += time;
- } else {
- dog.energy = 100;
- }
- return obj;
- };
- //// Prototypal Classes////
- const Dog = function (food, time){
- var obj = Object.create(Dog.prototype);
- obj.food = food;
- obj.time = time;
- return obj;
- };
- Dog.prototype.name: 'Scout',
- Dog.prototype.breed: ['Husky', 'German Shepherd'],
- Dog.prototype.age: 4,
- Dog.prototype.happiness: 50,
- Dog.prototype.hunger: 10,
- Dog.prototype.energy: 100,
- Dog.prototype.feed = function () {
- if(dog.hunger - food > 0) {
- dog.hunger -= food; // i wonder if it should be this.dog.hunger -= food?
- } else {
- dog.hunger = 0; // this.dog.hunger?
- }
- };
- Dog.prototype.play = function () {
- if(dog.happiness + time < 100) {
- dog.happiness += time; //this.dog.happiness?
- } else {
- dog.happiness = 100; //this?
- }
- if(dog.energy - time > 0) {
- dog.energy -= time; //this? etc.
- } else {
- dog.energy = 0;
- }
- };
- Dog.prototype.nap = function () {
- if(dog.energy + time < 100) {
- dog.energy += time;
- } else {
- dog.energy = 100;
- }
- };
- ///Psuedoclassical Classes /////
- const Dog = function (food, time) {
- this.feed = function (food) {
- if(dog.hunger - food > 0) {
- dog.hunger -= food;
- } else {
- dog.hunger = 0;
- }
- };
- this.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;
- }
- };
- this.nap = function(time) {
- if(dog.energy + time < 100) {
- dog.energy += time;
- } else {
- dog.energy = 100;
- }
- }
- };
- Dog.prototype.name: 'Scout',
- Dog.prototype.breed: ['Husky', 'German Shepherd'],
- Dog.prototype.age: 4,
- Dog.prototype.happiness: 50,
- Dog.prototype.hunger: 10,
- Dog.prototype.energy: 100,
Add Comment
Please, Sign In to add comment