Skip to content

Tom griggs #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
142 changes: 130 additions & 12 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
/*
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 hierarchy.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
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.
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 properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
* name
=== 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(props){ //setup of constructor function & this properties
this.createdAt = props.createdAt;
this.name = props.name;
this.dimensions = props.dimensions;

}

GameObject.prototype.destroy = function(){ //did this for memory reasons
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(characterProps){
GameObject.call(this, characterProps);
this.healthPoints = characterProps.healthPoints;

}
CharacterStats.prototype = Object.create(GameObject.prototype); //copy the prototype function from GameObject

CharacterStats.prototype.takeDamage = function(){

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


};


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,7 +59,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

}
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.
Expand All @@ -41,7 +80,7 @@

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

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +141,88 @@
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!
// * 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!
const goodGuy = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 100,
damage: 5,
name: 'Michael Scott',
team: 'Office',
weapons: [
'Rock',
'Hammer',
],
language: 'English',
});

const badGuy = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 100,
damage: 5,
name: 'Toby',
team: 'HR',
weapons: [
'Pen',

],
language: 'English',


});

function Villain(villainProps){
Humanoid.call(this, villainProps);
this.damage = villainProps.damage;
}

Villain.prototype.attack = function(){


return health = goodGuy.healthPoints - goodGuy.damage;

if(health > 0){return `${this.name} hurt ${goodGuy.name} & decreased his point total ${this.healthPoints}`}
else{return `${this.name} has vanquished ${goodGuy.name}`}
}




function Hero(heroProps){
Humanoid.call(this, heroProps);
this.damage = heroProps.damage;

}

Hero.prototype.attack = function(){
let health = this.healthPoints - this.damage;
if(health > 0){return `${this.name} hurt ${badGuy.name} & decreased his point total ${this.healthPoints}`}
else{return `${this.name} has vanquished ${badGuy.name}`}


}


console.log(goodGuy.attack());
console.log(badGuy.attack());
console.log(goodGuy.attack());
console.log(badGuy.attack());




75 changes: 68 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,87 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding- When the "this" keyword is in the global scope then it is referring to just that---the global scope object or
sometimes called the window object. This is where all of JavaScript is housed. Generally, this can be avoided by typing in "use strict" at the
beginning of your code.

* 2. Implicit Binding- This is the most common use of the "this" keyword & the one that makes the most
sense to me. When the "this" keyword is housed inside of an object it is initially referencing that object until another
function is invoked using the "dot" invoking notation. Whatever is to the left of the dot is what "this" will now be referencing.

* 3. New Binding - These are used with constructor functions. The "this" keyword refers to what the keyword "new" is referencing.

* 4. Explicit Binding- Similar to new binding in that it can involve the keyword "new"; however, other keywords (call, apply, bind) are used
to override what the "this" keyworod is initially pointing to.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

function dontDoThis(){
console.log(this); //this isn't pointing to anything in particular which means it is point to global obj
};
// Principle 2

// code example for Implicit Binding
const exampleObject = obj =>{ //function receiving an object as an argument


location: "Nasvhille", //define Obj properites
obj.greeting = function(name){

return(`Hello, my name is ${name} & I live in ${this.location}`) //return statement
console.log(this);
}
};



const anotherObject = { //create another object this can point to

location: "texas"

} ;
exampleObject(anotherObject);

console.log(anotherObject.greeting("Al")); //pass another argument to name when calling the functions


// Principle 3

// code example for New Binding
function Question(teacher){ //constructor function
this.teacher = teacher;
this.question = " asks: what is the answer to this problem?"; //define this properites inside constructor function

this.speak = function(){
console.log(this.teacher + this.question); //definie speak property
console.log(this);
}
}

const student = new Question("Mr. Griggs"); //definie variables that used the new keyword & tells us what 'this' is
const instructor = new Question("Student");

student.speak(); //call the functions
instructor.speak();
// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

function Conversation(personOne){ //Constructor function
this.person = personOne; //define properties ??
this.talk = " do you know the time?";

this.speaks = function(){ //define speaks function
console.log(this.person + this.talk);
console.log(this);
}
}

const alaina = new Conversation("Excuse me sir, "); //define variable with "new" keyword which tells what "this" is
const dene = new Conversation("Excuse me ma'am, ");

alaina.speaks.call(dene); //call the function but use the .call or .apply method to switch what 'this' is
dene.speaks.apply(alaina);