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
35 changes: 31 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,30 @@
* 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(attrs){
this.createdAt = attrs.createdAt;
this.name = attrs.name;
this.dimensions = attrs.dimensions;
}
GameObject.prototype.destroy = function(remove){
console.log(`${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(healthPoints){
GameObject.call(this, healthPoints);
this.damageAmount = healthPoints.damageAmount;
}
CharacterStats.prototype = Object.create (GameObject.prototype);
CharacterStats.prototype.takeDamage = function(){
console.log(`${this.name} took damage`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love seeing that you are checking your work before you return it. Shows that you care about quality.

return `${this.name} took damage`;
};


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

function Humanoid(human){
CharacterStats.call(this, human);
this.team = human.team;
this.weapons = human.weapons;
this.language = human.language;
}
Humanoid.prototype = Object.create(CharacterStats.prototype);
Humanoid.prototype.greet = function(greeting){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have a greeting parameter but never called it? Maybe it could have just been left out?

console.log(`${this.name} says hello in ${this.language}`);
return `${this.name} says hello 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 +68,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 +129,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
42 changes: 33 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* 1. This keyword binds to the global scope of an window/object
* 2. Implicit binding to the object left of the "."
* 3. "new" is a keyword used to create a constructer, this will point to that object.
* 4. Explicit binding makes use of .bind, .call, .apply which .this is defined.

* write out a code example of each explanation above
*/

// Principle 1

function stranger(name) {
console.log(this);
return name;
}
stranger("Ricardo");
// code example for Window Binding

// Principle 2

// Principle 2
const thisObj = {
color: 'red',
startRace: function (name) {
console.log(`The light is ${this.color}, ${name}`);
console.log(this);
}
};
thisObj.startRace('Not Yet!');
// code example for Implicit Binding

// Principle 3

function Fighter(name) {
this.arms = '22 inch arms';
this.height = '6ft tall';
this.stats = function () {
console.log(this.arms + " " + this.height);
console.log(this);
};
}
const Tyson = new Fighter('Mike Tyson');
const Holifield = new Fighter('Evander Holyfield');
Tyson.stats();
Holifield.stats();
// code example for New Binding

// Principle 4

Holifield.stats.call(Tyson);
Tyson.stats.apply(Holifield);
// code example for Explicit Binding