Guest User

Untitled

a guest
Oct 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. //// Functional Class (with duplicate methods - I know this can be taken further though)
  2. const Dog = function (food, time){
  3. var obj = {
  4. name: 'Scout',
  5. breed: ['Husky', 'German Shepherd'],
  6. age: 4,
  7. happiness: 50,
  8. hunger: 10,
  9. energy: 100,
  10. }
  11. obj.feed = function {
  12. if(dog.hunger - food > 0) {
  13. dog.hunger -= food;
  14. } else {
  15. dog.hunger = 0;
  16. }
  17. obj.play = function {
  18. if(dog.happiness + time < 100) {
  19. dog.happiness += time;
  20. } else {
  21. dog.happiness = 100;
  22. }
  23. obj.nap = function {
  24. if(dog.energy + time < 100) {
  25. dog.energy += time;
  26. } else {
  27. dog.energy = 100;
  28. }
  29. return obj;
  30. };
  31.  
  32. //// Prototypal Classes////
  33.  
  34. const Dog = function (food, time){
  35. var obj = Object.create(Dog.prototype);
  36. obj.food = food;
  37. obj.time = time;
  38. return obj;
  39. };
  40.  
  41. Dog.prototype.name: 'Scout',
  42. Dog.prototype.breed: ['Husky', 'German Shepherd'],
  43. Dog.prototype.age: 4,
  44. Dog.prototype.happiness: 50,
  45. Dog.prototype.hunger: 10,
  46. Dog.prototype.energy: 100,
  47. Dog.prototype.feed = function () {
  48. if(dog.hunger - food > 0) {
  49. dog.hunger -= food; // i wonder if it should be this.dog.hunger -= food?
  50. } else {
  51. dog.hunger = 0; // this.dog.hunger?
  52. }
  53. };
  54. Dog.prototype.play = function () {
  55. if(dog.happiness + time < 100) {
  56. dog.happiness += time; //this.dog.happiness?
  57. } else {
  58. dog.happiness = 100; //this?
  59. }
  60.  
  61. if(dog.energy - time > 0) {
  62. dog.energy -= time; //this? etc.
  63. } else {
  64. dog.energy = 0;
  65. }
  66. };
  67. Dog.prototype.nap = function () {
  68. if(dog.energy + time < 100) {
  69. dog.energy += time;
  70. } else {
  71. dog.energy = 100;
  72. }
  73. };
  74.  
  75. ///Psuedoclassical Classes /////
  76.  
  77. const Dog = function (food, time) {
  78. this.feed = function (food) {
  79. if(dog.hunger - food > 0) {
  80. dog.hunger -= food;
  81. } else {
  82. dog.hunger = 0;
  83. }
  84. };
  85. this.play = function(time) {
  86. if(dog.happiness + time < 100) {
  87. dog.happiness += time;
  88. } else {
  89. dog.happiness = 100;
  90. }
  91. if(dog.energy - time > 0) {
  92. dog.energy -= time;
  93. } else {
  94. dog.energy = 0;
  95. }
  96. };
  97. this.nap = function(time) {
  98. if(dog.energy + time < 100) {
  99. dog.energy += time;
  100. } else {
  101. dog.energy = 100;
  102. }
  103. }
  104. };
  105.  
  106. Dog.prototype.name: 'Scout',
  107. Dog.prototype.breed: ['Husky', 'German Shepherd'],
  108. Dog.prototype.age: 4,
  109. Dog.prototype.happiness: 50,
  110. Dog.prototype.hunger: 10,
  111. Dog.prototype.energy: 100,
Add Comment
Please, Sign In to add comment