Last active
August 30, 2018 16:47
-
-
Save maxvolovikov/1ce8373708a8d1c4904e571e0ac0ffd5 to your computer and use it in GitHub Desktop.
Functional class of a dog object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Dogs = function (name, breed, age, happiness, hunger, energy){ | |
var dog = {name : name, breed : breed, age : age, happiness : happiness, hunger : hunger, energy : energy}; | |
extend(dog, Dogs.methods) | |
return dog; | |
} | |
Dogs.methods = { | |
feed : function(food){ | |
if(this.hunger - food > 0) { | |
this.hunger -= food; | |
} else { | |
this.hunger = 0; | |
} | |
}, | |
play : function(time){ | |
if(this.happiness + time < 100) { | |
this.happiness += time; | |
} else { | |
this.happiness = 100; | |
} | |
if(dog.energy - time > 0) { | |
dog.energy -= time; | |
} else { | |
dog.energy = 0; | |
} | |
}, | |
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