Skip to content
Open
Show file tree
Hide file tree
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
147 changes: 104 additions & 43 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
/*
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.

Expand All @@ -7,7 +8,7 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +17,37 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(player) {
this.createdAt = player.createdAt;
this.name = player.name;
this.dimensions = player.dimensions; {
this.length = player.length;
this.width = player.width;
this.height = player.height;
};
}
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(player) {
GameObject.call(this, player)
this.healthPoints = player.healthPoints;

};
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
Expand All @@ -32,79 +57,115 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(player) {
CharacterStats.call(this, player);
this.team = player.team,
this.weapons = player.weapons,
this.language = player.language;

};

Humanoid.prototype = Object.create(CharacterStats.prototype);

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

function Hero(player) {
Humanoid.call(this, player);

}

Hero.prototype.attack = function() {
Villian.call(takeDamage);
}

Hero.prototype = Object.create(Humanoid.prototype);

function Villian(player) {
Humanoid.call(this, player);

}

Villian.prototype = Object.create(Humanoid.prototype);

// Villian.prototype.attack = function() {
// Hero.call(takeDamage);
// let hero.healthPoints = hero.healthPoints--;
// }

/*
* 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.
*/
* 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({

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

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

const archer = new Humanoid({
const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
'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.
*/
});

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!
// 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!
45 changes: 33 additions & 12 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
*/
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. "Window Binding" the natural state that "this" will reference if left unspecified, will result in a return value of "undefined" unless in 'strict mode' which will cause an error.
* 2. "Implicit Binding" When invoked "this" will reference the object called to the left of the "." in the invocation.
* 3. "New Binding" When working with multiple objects with like properties, the "new" keyword can be used to invoke an object function to create a new object by passing in the values as parameters.
* 4. "Explicit Binding" This uses the keywords "call", "apply" & "bind" in the invoction to specify the object being referenced. "call" is used to add one argument at a time, while "apply" allows multiple arguments to be passed in. Both immediately invoke the function. "bind" is the same as "call" except instead of invoking the function it returns a new function to be called later.
*
* write out a code example of each explanation above
*/

// Principle 1

let catName = function() {
console.log(`${this.name}`);
};
catName(); // ===> returns 'undefined'
// code example for Window Binding

// Principle 2

let mrW = {
name: 'Mr. Whiskas',
greeting: 'Meow.',
sayHello: function() {
console.log(this.greeting);
}
};
mrW.sayHello();
// code example for Implicit Binding

// Principle 3
let Cat = function(name, greeting) {
this.name = name;
this.greeting = greeting;
};

let purr = new Cat('Purrsephone', 'Prrraawww...');
console.log(purr);
// code example for New Binding

// Principle 4

function sayHelloTwo() {
console.log(`${this.greeting} ${this.greeting}`)
}
sayHelloTwo.call(mrW);
let nowTwo = sayHelloTwo.bind(purr);
nowTwo();
// code example for Explicit Binding