Skip to content
Merged
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
151 changes: 125 additions & 26 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,87 @@
/*
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.
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 heirarchy.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.

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

/*
=== 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.`
* dimensions
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

function GameObject (objAttributes){
this.createdAt=objAttributes.createdAt;
this.dimensions=objAttributes.dimensions;
}
GameObject.prototype.destroy =function(){
return `${this.name} was removed from the game`;
}
/*
=== CharacterStats ===
* healthPoints
* hp
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(charAttributes){
this.hp=charAttributes.hp;
this.name=charAttributes.name;
GameObject.call (this, charAttributes);
}
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
=== Humanoid ===
* faction
* 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(huAttributes){
this.faction = huAttributes.faction;
this.weapons = huAttributes.weapons;
this.language = huAttributes.language;
CharacterStats.call (this, huAttributes);
}
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:
// Test you work by uncommenting 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,
hp: 5,
name: 'Bruce',
team: 'Mage Guild',
faction: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
language: 'Common Toungue',
});

const swordsman = new Humanoid({
Expand All @@ -65,14 +91,14 @@
width: 2,
height: 2,
},
healthPoints: 15,
hp: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
language: 'Common Toungue',
});

const archer = new Humanoid({
Expand All @@ -82,9 +108,9 @@
width: 2,
height: 4,
},
healthPoints: 10,
hp: 10,
name: 'Lilith',
team: 'Forest Kingdom',
faction: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
Expand All @@ -94,17 +120,90 @@

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(swordsman.faction); // 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!
// * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function.
function Villian(vilAttributes){
Humanoid.call(this, vilAttributes);
this.alias=vilAttributes.alias;
}
Villian.prototype = Object.create(Humanoid.prototype);
Villian.prototype.attack= function(character){
while(character.hp>0){
return character.hp-1;
}
return character.destroy();
}
function Hero(heroAttributes){
this.sadBackStory= heroAttributes.sadBackStory;
Humanoid.call(this,heroAttributes);
}
Hero.prototype = Object.create(Humanoid.prototype);
// * Give the Hero and Villians 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;
Hero.prototype.attack= function(character){
if(character.hp>0){
character.hp=character.hp-1;
return `${character.alias} is still alive`;
}
else{
return character.destroy();

}

}
Hero.prototype.stateBackStory=function(){
return this.sadBackStory;
}
Hero.prototype.justiceCall=function(character){
return `For justice I- ${this.name} will fight you ${character.alias}`;
}
// * Create two new objects, one a villian and one a hero and fight it out with methods!
const myHero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 5,
name: 'Bruce',
faction: 'Solo',
weapons: [
'rock',
'paper',
'scissors'
],
language: 'Common Toungue',
sadBackStory: "Mom died, dad died, then revenge and vow to protect justice",
});
const myVillian = new Villian({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
hp: 2,
name: 'Sir Mustachio',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Toungue',
alias: "The Pimples Popper"
});
console.log(myHero.stateBackStory());
console.log(myHero.justiceCall(myVillian));
console.log(myHero.attack(myVillian));
console.log(myHero.attack(myVillian));
console.log(myHero.attack(myVillian));
console.log(myHero.attack(myVillian));
49 changes: 36 additions & 13 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
/* 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 is when code is executed using "this" keyword as part of simple function call then it refers to global or window object in case of browser. It is also default binding.
* 2. Implicit Binding is when a method is called as a property of object, then "this" refers to the parent object of that method.
* 3. New Binding is when a function is called with "new" keyword which is known as constrctor function, then "this" refers to that newly created instance.
* 4. Explicit Binding is when a function is called using call, apply, and bind method then "this" refers to the value passed as first argument of call, apply or bind method.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function windowBinding() {
console.log(this);
}
windowBinding();

// Principle 2

// code example for Implicit Binding
const person = {
firstName: "Sterling",
lastName: "Ford",
fullName: function() {
console.log(`Name: ${this.firstName} ${this.lastName}`);
}
};
person.fullName();

// Principle 3

// code example for New Binding
function DCSuperhero(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function() {
console.log(`Name: ${this.firstName} ${this.lastName}`);
};
}
const arrow = new DCSuperhero("Oliver", "Queen");
const flash = new DCSuperhero("Barry", "Allen");
const supergirl = new DCSuperhero("Kara", "Zor-El");
arrow.fullName();

// Principle 4
// code example for Explicit Binding
//kept the same example as principle 3 - New Binding

// code example for Explicit Binding
arrow.fullName.call(flash);
flash.fullName.apply(supergirl);