Skip to content
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ This challenge focuses on using the `this` keyword as well as getting comfortabl

## Git Set up

* [ ] Fork the project into your GitHub user account
* [ ] Clone the forked project into a directory on your machine
* [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [ ] You are now ready to build this project with your preferred IDE
* [ x] Fork the project into your GitHub user account
* [ x] Clone the forked project into a directory on your machine
* [x ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [x. ] You are now ready to build this project with your preferred IDE

## Assignment Set Up

Expand Down
57 changes: 33 additions & 24 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@
Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

/*
=== CharacterStats ===
* healthPoints
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function GameObject (attrs) {
this.createdAt = attrs.createdAt;
this.dimensions = attrs.dimensions;
}
Comment thread
diamondpowell marked this conversation as resolved.
Resolved
GameObject.prototype.destroy = function () {
return `${this.name} was removed from the game.`;
}

/*
=== 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 CharacterStats (attrs1) {
GameObject.call(this,attrs1);
this.healthPoints = attrs1.healthPoints;
this.name = attrs1.name;
}
CharacterStats.prototype = Object.create(GameObject.prototype);

CharacterStats.prototype.takeDamage = function () {
return `${this.name} took damage.`;
}

Comment thread
diamondpowell marked this conversation as resolved.
Resolved


function Humanoid (attrs2) {
CharacterStats.call(this,attrs2);
this.team = attrs2.team;
this.weapons = attrs2.weapons;
this.language = attrs2.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
Expand All @@ -41,7 +50,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,7 +111,7 @@
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.
Expand Down
54 changes: 47 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. window binding - its usually the default behavior
* 2. implicit binding - left of the period when it is invoked
* 3. explicit binding - lets you use the call or apply function to pass a reference
* 4. new binding - using the new keyword to call the object that was created
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

function howManyToes(toes) {
console.log(this);
return toes;
}
howManyToes('12');
// Principle 2

// code example for Implicit Binding
const myObj = {
place: 'Atlanta',
city: function(state) {
console.log(`${this.place} is in ${state}`);
console.log(this);
}
};
myObj.city('Georgia');

// Principle 3

// code example for New Binding

// function ShoeDescription(sneaker) {
// this.description = 'Nice ';
// this.type = sneaker;
// this.apply = function() {
// console.log(this.description + this.type);
// console.log(this);
// };
// }

// const nikes = new ShoeDescription('reeboks');
// const reeboks = new ShoeDescription('nikes');

// nikes.apply();
// reeboks.apply();
// Principle 4

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

function ShoeDescription(sneaker) {
this.description = 'Nice ';
this.type = sneaker;
this.apply = function() {
console.log(this.description + this.type);
console.log(this);
};
}

const nikes = new ShoeDescription('reeboks');
const reeboks = new ShoeDescription('nikes');

nikes.apply.call(reeboks);
reeboks.apply.apply(nikes);