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
40 changes: 38 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
Each constructor function has unique properties and methods that are defined in their block comments below:
*/

function GameObject(attributes){
this.createdAt = attributes.createdAt;
this.dimensions = attributes.dimensions;
}

GameObject.prototype.destroy = function () {
return 'Object was removed from the game.';
};

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +25,39 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function CharacterStats(charAttributes) {
GameObject.call(this, charAttributes);
this.hp = charAttributes.hp;
this.name = charAttributes.name;
this.healthPoints = charAttributes.healthPoints;
}

CharacterStats.prototype = Object.create(GameObject.prototype);

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

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

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

Humanoid.prototype = Object.create(CharacterStats.prototype);

Humanoid.prototype.greet = function () {
return `${this.name} offers a greeting in ${this.language}.`;
};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -33,6 +68,8 @@
* should inherit takeDamage() from CharacterStats
*/



/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +78,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 +139,6 @@
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
39 changes: 34 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* 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-.
* 2. implicit binding-.
* 3. explicit binding-.
* 4. new binding-.
*
* write out a code example of each explanation above
*/
Expand All @@ -17,10 +17,39 @@

// code example for Implicit Binding

let Josh = {
name: 'Josh',
favorite_show: 'Berserk',
age: function () {
console.log(this.age);
}
}

Josh.age(43);

// Principle 3

// code example for New Binding

function josh(name) {
this.name = name;
}

let host = new josh('Hello my name is Josh');

console.log(Josh.name);

// Principle 4

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

function josh() {
console.log(this.age);
}

let new_Josh = {
name: 'Josh',
favorite_show: 'Berserk',
}

josh.call(new_Josh);