Skip to content

Commit 84c7efe

Browse files
Javascript-III assignment Done
1 parent 5140444 commit 84c7efe

1 file changed

Lines changed: 46 additions & 13 deletions

File tree

‎assignments/prototypes.js‎

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
* dimensions
1515
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
1616
*/
17+
function GameObject(attributes) {
18+
this.createdAt = attributes.createdAt;
19+
this.dimensions = attributes.dimensions;
20+
}
21+
22+
GameObject.prototype.destroy = function() {
23+
return `Object was removed from the game.`;
24+
}
1725

1826
/*
1927
=== CharacterStats ===
@@ -22,6 +30,18 @@
2230
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
2331
* should inherit destroy() from GameObject's prototype
2432
*/
33+
function CharacterStats(charAttributes) {
34+
this.hp = charAttributes.hp;
35+
this.name = charAttributes.name;
36+
GameObject.call(this, charAttributes);
37+
}
38+
39+
CharacterStats.prototype = Object.create(GameObject.prototype);
40+
41+
CharacterStats.prototype.takeDamage = function() {
42+
return `${this.name} took damage`;
43+
}
44+
2545

2646
/*
2747
=== Humanoid ===
@@ -32,7 +52,20 @@
3252
* should inherit destroy() from GameObject through CharacterStats
3353
* should inherit takeDamage() from CharacterStats
3454
*/
35-
55+
function Humanoid(humanoidAttributes) {
56+
this.faction = humanoidAttributes.faction;
57+
this.weapons = humanoidAttributes.weapons;
58+
this.language = humanoidAttributes.language;
59+
CharacterStats.call(this,humanoidAttributes);
60+
61+
}
62+
63+
Humanoid.prototype = Object.create(CharacterStats.prototype);
64+
65+
Humanoid.prototype.greet = function() {
66+
return `${this.name} offers a greeting in ${this.language}`;
67+
}
68+
3669
/*
3770
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
3871
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
@@ -41,7 +74,7 @@
4174

4275
// Test you work by uncommenting these 3 objects and the list of console logs below:
4376

44-
/*
77+
4578
const mage = new Humanoid({
4679
createdAt: new Date(),
4780
dimensions: {
@@ -92,17 +125,17 @@
92125
language: 'Elvish',
93126
});
94127

95-
console.log(mage.createdAt); // Today's date
96-
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
97-
console.log(swordsman.hp); // 15
98-
console.log(mage.name); // Bruce
99-
console.log(swordsman.faction); // The Round Table
100-
console.log(mage.weapons); // Staff of Shamalama
101-
console.log(archer.language); // Elvish
102-
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103-
console.log(mage.takeDamage()); // Bruce took damage.
104-
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
128+
console.log("1.... " +mage.createdAt); // Today's date
129+
console.log("2.... "+archer.dimensions); // { length: 1, width: 2, height: 4 }
130+
console.log("3.... "+swordsman.hp); // 15
131+
console.log("4.... "+mage.name); // Bruce
132+
console.log("5.." +swordsman.faction); // The Round Table
133+
console.log("6... "+mage.weapons); // Staff of Shamalama
134+
console.log("7..." +archer.language); // Elvish
135+
console.log("8..." +archer.greet()); // Lilith offers a greeting in Elvish.
136+
console.log("9..." +mage.takeDamage()); // Bruce took damage.
137+
console.log("10.."+swordsman.destroy()); // Sir Mustachio was removed from the game.
138+
106139

107140
// Stretch task:
108141
// * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function.

0 commit comments

Comments
 (0)