Skip to content

Commit 933be7f

Browse files
author
Sascha
committed
added prototype.js without stretchgoal
1 parent 92f8a0f commit 933be7f

1 file changed

Lines changed: 110 additions & 4 deletions

File tree

assignments/prototypes.js

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,31 @@
1515
* dimensions (These represent the character's size in the video game)
1616
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
1717
*/
18+
function GameObject(gameObjectAttributes) {
19+
this.createdAt = gameObjectAttributes.createdAt;
20+
this.name = gameObjectAttributes.name;
21+
this.dimensions = gameObjectAttributes.dimensions;
22+
}
23+
24+
GameObject.prototype.destroy = function () {
25+
return `${this.name} was removed from the game.`;
26+
};
1827

1928
/*
2029
=== CharacterStats ===
2130
* healthPoints
2231
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
2332
* should inherit destroy() from GameObject's prototype
2433
*/
34+
function CharacterStats(characterStatsAttributes) {
35+
GameObject.call(this, characterStatsAttributes);
36+
this.healthPoints = characterStatsAttributes.healthPoints;
37+
}
38+
39+
CharacterStats.prototype = Object.create(GameObject.prototype)
40+
CharacterStats.prototype.takeDamage = function() {
41+
return `${this.name} took damage`;
42+
}
2543

2644
/*
2745
=== Humanoid (Having an appearance or character resembling that of a human.) ===
@@ -32,7 +50,18 @@
3250
* should inherit destroy() from GameObject through CharacterStats
3351
* should inherit takeDamage() from CharacterStats
3452
*/
35-
53+
function Humanoid(humanoidAttributes) {
54+
CharacterStats.call(this, humanoidAttributes)
55+
this.team = humanoidAttributes.team;
56+
this.weapons = humanoidAttributes.weapons;
57+
this.language = humanoidAttributes.language
58+
GameObject.call(this, humanoidAttributes)
59+
}
60+
Humanoid.prototype = Object.create(CharacterStats.prototype)
61+
Humanoid.prototype.greet = function () {
62+
return `${this.name} offers a greeting in ${this.language}.`
63+
};
64+
3665
/*
3766
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
3867
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
@@ -41,7 +70,7 @@
4170

4271
// Test you work by un-commenting these 3 objects and the list of console logs below:
4372

44-
/*
73+
4574
const mage = new Humanoid({
4675
createdAt: new Date(),
4776
dimensions: {
@@ -102,9 +131,86 @@
102131
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103132
console.log(mage.takeDamage()); // Bruce took damage.
104133
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
134+
106135

107136
// Stretch task:
108137
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109138
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
139+
// * Create two new objects, one a villain and one a hero and fight it out with methods!
140+
141+
142+
143+
/* function Player(name) {
144+
this.name = name
145+
}
146+
147+
Person.prototype.sayName = function sayName() {
148+
console.log(`My name is ${this.name} I am team ${this.team}`);
149+
150+
function GreenTeamPlayer(name) {
151+
Player.call(this, name)
152+
this.team = 'green'
153+
}
154+
155+
GreenTeamPlayer.prototype = Object.create(Player.prototype)
156+
157+
function RedTeamPlayer(name) {
158+
Player.call(this, name)
159+
this.team = 'red'
160+
}
161+
162+
RedTeamPlayer.prototype = Object.create(Player.prototype)
163+
164+
const dan = new GreenTeamPlayer('Dan')
165+
const cat = new RedTeamPlayer('Rosie')
166+
167+
dan.name()
168+
cat.sayName()
169+
*/
170+
171+
/*
172+
# Prototypes Challenge
173+
174+
Overview: Study the console.log() and object method invocations at the bottom of the page. Update the Animal and Dog constructors so that the logs and methods match the commented result next to them.
175+
176+
Challenge: Add the missing `speak` method, complete the `eat` method
177+
*/
178+
179+
/*
180+
function Animal(attributes) {
181+
this.weight = attributes.weight;
182+
this.height = attributes.height;
183+
this.food = attributes.food;
184+
}
185+
186+
Animal.prototype.eat = function() {
187+
console.log(`The ${this.animalCommonName} eats ${this.food}`);
188+
}
189+
190+
Animal.prototype.speak = function() {
191+
console.log(`${this.name} says: ${this.bark}`);
192+
}
193+
194+
function Dog(dogAttributes) {
195+
// Connect the attributes so we can use the this keyword
196+
Animal.call(this, dogAttributes);
197+
this.name = dogAttributes.name;
198+
this.bark = dogAttributes.bark;
199+
this.animalCommonName = dogAttributes.animalCommonName;
200+
}
201+
// Set up our __proto__ inheritance to Animal
202+
Dog.prototype = Object.create(Animal.prototype);
203+
204+
const dog = new Dog({
205+
'name': 'Dr. Doggo',
206+
'animalCommonName': "dog",
207+
'weight': 40,
208+
'height': 12,
209+
'food': 'meat',
210+
'bark': 'Woof!'
211+
});
212+
213+
console.log(dog.animalCommonName); // "dog"
214+
dog.eat(); // "The dog eats meat"
215+
dog.speak(); // "Dr. Doggo says: Woof!"
216+
*/

0 commit comments

Comments
 (0)