Skip to content

Commit 2ac4561

Browse files
committed
Refactored into classes.
1 parent 04f8e3b commit 2ac4561

1 file changed

Lines changed: 59 additions & 25 deletions

File tree

assignments/prototype-refactor.js

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,73 @@
22
// Today your goal is to refactor all of this code to use ES6 Classes.
33
// The console.log() statements should still return what is expected of them.
44

5-
function GameObject(options) {
6-
this.createdAt = options.createdAt;
7-
this.dimensions = options.dimensions;
8-
}
95

10-
GameObject.prototype.destroy = function() {
11-
return `Object was removed from the game.`;
12-
};
136

14-
function CharacterStats(characterStatsOptions) {
15-
GameObject.call(this, characterStatsOptions);
16-
this.hp = characterStatsOptions.hp;
17-
this.name = characterStatsOptions.name;
7+
class GameObject {
8+
constructor(options){
9+
this.createdAt = options.createdAt;
10+
this.dimensions = options.dimensions;
11+
}// Methods go here:
12+
destroy() {
13+
return `${this.name} was removed from the game.`;
14+
}
15+
} // End GameObject
16+
17+
// function GameObject(options) {
18+
// this.createdAt = options.createdAt;
19+
// this.dimensions = options.dimensions;
20+
// }
21+
// GameObject.prototype.destroy = function() {
22+
// return `Object was removed from the game.`;
23+
// };
24+
25+
class CharacterStats extends GameObject {
26+
constructor(characterStatsOptions){
27+
super(characterStatsOptions);
28+
this.hp = characterStatsOptions.hp;
29+
this.name = characterStatsOptions.name;
30+
}// Methods go here:
31+
takeDamage(){
32+
return `${this.name} took damage.`;
1833
}
34+
}// End CharacterStats Class
1935

20-
CharacterStats.prototype = Object.create(GameObject.prototype);
2136

22-
CharacterStats.prototype.takeDamage = function() {
23-
return `${this.name} took damage.`;
24-
};
37+
// function CharacterStats(characterStatsOptions) {
38+
// GameObject.call(this, characterStatsOptions);
39+
// this.hp = characterStatsOptions.hp;
40+
// this.name = characterStatsOptions.name;
41+
// }
2542

26-
function Humanoid(humanoidOptions) {
27-
CharacterStats.call(this, humanoidOptions);
28-
this.faction = humanoidOptions.faction;
29-
this.weapons = humanoidOptions.weapons;
30-
this.language = humanoidOptions.language;
31-
}
43+
// CharacterStats.prototype = Object.create(GameObject.prototype);
44+
45+
// CharacterStats.prototype.takeDamage = function() {
46+
// return `${this.name} took damage.`;
47+
// };
48+
49+
class Humanoid extends CharacterStats {
50+
constructor(humanoidOptions){
51+
super(humanoidOptions);
52+
this.faction = humanoidOptions.faction;
53+
this.weapons = humanoidOptions.weapons;
54+
this.language = humanoidOptions.language;
3255

33-
Humanoid.prototype = Object.create(CharacterStats.prototype);
56+
}// Methods go here:
57+
greet(){
58+
return `${this.name} offers a greeting in ${this.language}.`;
59+
}
60+
} // End Humanoid Class
3461

35-
Humanoid.prototype.greet = function() {
36-
return `${this.name} offers a greeting in ${this.language}.`;
37-
};
62+
// function Humanoid(humanoidOptions) {
63+
// CharacterStats.call(this, humanoidOptions);
64+
// this.faction = humanoidOptions.faction;
65+
// this.weapons = humanoidOptions.weapons;
66+
// this.language = humanoidOptions.language;
67+
// }
68+
// Humanoid.prototype = Object.create(CharacterStats.prototype);
69+
// Humanoid.prototype.greet = function() {
70+
// return `${this.name} offers a greeting in ${this.language}.`;
71+
// };
3872

3973
const mage = new Humanoid({
4074
createdAt: new Date(),

0 commit comments

Comments
 (0)