Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 270 additions & 1 deletion assignments/prototype-refactor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,278 @@


/*

Prototype Refactor

1. Copy and paste your code or the solution from yesterday
/*
=== GameObject ===
* createdAt
* name
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

// function GameObject(properties) {
// this.createdAt = properties.createdAt;
// this.name = properties.name;
// this.dimensions = properties.dimensions;

// }
// GameObject.prototype.destroy = function() {
// return `${this.name} was removed from the game.`
// }

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
// function CharacterStats(stats) {
// this.healthPoints = stats.healthPoints;

// GameObject.call(this, stats)
// }
// CharacterStats.prototype = Object.create(GameObject.prototype);
// CharacterStats.prototype.takeDamage = function() {
// return `${this.name} took damage.`;
// }
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
// function Humanoid(attributes) {
// this.team = attributes.team;
// this.weapons = attributes.weapons;
// this.language = attributes.language;

2. Your goal is to refactor all of this code to use ES6 Classes. The console.log() statements should still return what is expected of them.
// CharacterStats.call(this, attributes)
// }

// Humanoid.prototype = Object.create(CharacterStats.prototype)
// Humanoid.prototype.greet = function() {
// return `${this.name} offers a greeting in ${this.language}.`
// }

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
* Instances of CharacterStats should have all of the same properties as GameObject.
*/

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


// const mage = new Humanoid({
// createdAt: new Date(),
// dimensions: {
// length: 2,
// width: 1,
// height: 1,
// },
// healthPoints: 5,
// name: 'Bruce',
// team: 'Mage Guild',
// weapons: [
// 'Staff of Shamalama',
// ],
// language: 'Common Tongue',
// });

// const swordsman = new Humanoid({
// createdAt: new Date(),
// dimensions: {
// length: 2,
// width: 2,
// height: 2,
// },
// healthPoints: 15,
// name: 'Sir Mustachio',
// team: 'The Round Table',
// weapons: [
// 'Giant Sword',
// 'Shield',
// ],
// language: 'Common Tongue',
// });

// const archer = new Humanoid({
// createdAt: new Date(),
// dimensions: {
// length: 1,
// width: 2,
// height: 4,
// },
// healthPoints: 10,
// name: 'Lilith',
// team: 'Forest Kingdom',
// weapons: [
// 'Bow',
// 'Dagger',
// ],
// language: 'Elvish',
// });

// console.log(mage.createdAt); // Today's date
// console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
// console.log(swordsman.healthPoints); // 15
// console.log(mage.name); // Bruce
// console.log(swordsman.team); // The Round Table <=
// console.log(mage.weapons); // Staff of Shamalama <=
// console.log(archer.language); // Elvish <=
// console.log(archer.greet()); // Lilith offers a greeting in Elvish. <=
// console.log(mage.takeDamage()); // Bruce took damage.
// console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
// * 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;
// * Create two new objects, one a villain and one a hero and fight it out with methods!


//2. Your goal is to refactor all of this code to use ES6 Classes. The console.log() statements should still return what is expected of them.


// function GameObject(properties) {
// this.createdAt = properties.createdAt;
// this.name = properties.name;
// this.dimensions = properties.dimensions;

// }
// GameObject.prototype.destroy = function() {
// return `${this.name} was removed from the game.`
// }

class GameObject {
constructor(createdAt, name, dimensions) {


this.createdAt = createdAt;
this.name = name;
this.dimensions = dimensions;
}

destroy() { return `${this.name} was removed from the game.` }

}

// function CharacterStats(stats) {
// this.healthPoints = stats.healthPoints;

// GameObject.call(this, stats)
// }
// CharacterStats.prototype = Object.create(GameObject.prototype);
// CharacterStats.prototype.takeDamage = function() {
// return `${this.name} took damage.`;
// }

class CharacterStats extends GameObject {
constructor(healthPoints) {
super(healthPoints);

this.healthPoints = healthPoints;

super.destroy();
};

takeDamage() { return `${this.name} took damage.`; }
}


// function Humanoid(attributes) {
// this.team = attributes.team;
// this.weapons = attributes.weapons;
// this.language = attributes.language;

// CharacterStats.call(this, attributes)
// }

// Humanoid.prototype = Object.create(CharacterStats.prototype)
// Humanoid.prototype.greet = function() {
// return `${this.name} offers a greeting in ${this.language}.`
// }

class Humanoid extends CharacterStats {
constructor(team, weapons, language) {
super(team, weapons, language);


this.team = team;
this.weapons = weapons;
this.language = language;
super.destroy();
super.takeDamage();
}
greet() { return `${super.name} offers a greeting in ${super.language}.`}
}

//Object.setPrototypeOf(GameObject, CharacterStats);

// New Characters:
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table <=
console.log(mage.weapons); // Staff of Shamalama <=
console.log(archer.language); // Elvish <=
console.log(archer.greet()); // Lilith offers a greeting in Elvish. <=
console.log(mage.takeDamage()); // Bruce took damage.
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.