Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vinnihoke/7a6f6ac19770e0d9280647bb500da2fd to your computer and use it in GitHub Desktop.
Save vinnihoke/7a6f6ac19770e0d9280647bb500da2fd to your computer and use it in GitHub Desktop.
*
=== 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(createdAt, name, dimensions){
this.createdAt = createdAt;
this.name = name;
this.dimensions = dimensions;
}
GameObject.prototype.destroy = function(){
console.log(`${this.name} was removed from the game.`);
}
CharacterStats.prototype = Object.create(GameObject.prototype);
/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(createdAt, name, dimensions, healthPoints) {
GameObject.call(this, createdAt, name, dimensions);
this.healthPoints = healthPoints;
};
CharacterStats.prototype.takeDamage = function(){
console.log(`${this.name} took damage.`);
};
Humanoid.prototype = Object.create(CharacterStats.prototype);
/*
=== 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(createdAt, name, dimensions, healthPoints, team, weapons, language) {
CharacterStats.call(this, healthPoints, createdAt, name, dimensions);
this.team = team;
this.weapons = weapons;
this.language = language;
}
Humanoid.prototype.greet = function () {
console.log(`${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());
@vinnihoke
Copy link
Author

I'm just confused about the parent child relationship in this assignment. I feel like Humanoid needs to be first, then inherit the others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment