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
55 changes: 52 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,35 @@
* 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(attributes) {
this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.dimensions;
}
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 (stats) {
GameObject.call(this, stats);
this.healthPoints = stats.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,6 +53,34 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/



function Humanoid (human) {
GameObject.call(this, 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 () {
return `${this.name} offers a greeting in ${this.language}.`;
};


/* Stretch Goals */

function Heros (hero) {
GameObject.call(this, hero);
Humanoid.call(this, hero);
CharacterStats.call(this, hero);

}


/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +90,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 +151,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
57 changes: 50 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,69 @@
/* The for principles of "this";
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1.Global Binding - the 'this' is referencing the window object. Javascript has a Global context

* 2.Implicit Binding - When a function is called by a preceding dot, that object before the '.' is what 'this' is referring to.

* 3.New Binding - involves a constructor function. Refers to the newly created instance of the Object, which is then returned.

* 4.Explicit Binding - Refers to whenever JS's call or apply method is used.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

function newMan(name) {
console.log(this);
console.log (`Look, it's the new guy, ${name}!`)
}
newMan("Kyle");
// Principle 2

// code example for Implicit Binding
const joke = {
question: 'Why did the chicken cross the road?',
punchLine: function (punch) {
console.log(`${this.question}`);
console.log(`${punch}`);
}
};
joke.punchLine('To get to the other side!');

// Principle 3

// code example for New Binding
function funnyGuy(comedian) {
this.joke = 'So i walk into a bar....';
this.comedian = `Joke by ${comedian}`;
this.pitch = function() {
console.log(this.joke + this.comedian);
console.log(this)
};
}

const kyle = new funnyGuy('Kyle');
const bob = new funnyGuy('Bob');

console.log(kyle.pitch());
console.log(bob.pitch());

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function funnyGuy(comedian) {
this.joke = 'So i walk into a bar....';
this.comedian = `Joke by ${comedian}`;
this.pitch = function() {
console.log(this.joke + this.comedian);
console.log(this)
};
}

const jim = new funnyGuy('Jim');
const joe = new funnyGuy('Joe');

console.log(jim.pitch.call(joe));
console.log(joe.pitch.apply(jim));