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
37 changes: 35 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
* 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 ===
Expand All @@ -23,6 +32,16 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(attributes) {
GameObject.call(this, attributes);
this.healthPoints = attributes.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 @@ -33,6 +52,20 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attributes) {
CharacterStats.call(this, attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.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 +74,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 +135,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
39 changes: 34 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
/* 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 in the global scope, the 'this' keyword will show all of the window object.
* 2. Implicit Binding: When calling a function. 'this' refers to the object in the dot notation.
* 3. New binding: When an object is created using the new keyword, the 'this' keyword will refer to the new object.
* 4. Explicit binding: when using call or apply, 'this' is overwritten to whatever object is called.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function speak(words) {
console.log(this);
return words;
}

speak("hello");

// Principle 2

// code example for Implicit Binding
const obj = {
hometown: 'Burlington',
origin: function(profession) {
console.log(`Hello, I am a ${profession} from ${this.hometown}`);
}
};
obj.origin('developer');

// Principle 3

// code example for New Binding

function Developer(name, language) {
this.name = name;
this.favoriteLanguage = language;
this.speak = function() {
console.log(`My name is ${this.name} and my favorite language is ${this.favoriteLanguage}`)
};
}

const matt = new Developer('Matt', 'Javascript');
matt.speak();
const ben = new Developer('Ben', 'Python');
ben.speak();

// Principle 4

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

matt.speak.call(ben); // this changes to ben
ben.speak.call(matt); // this changes to matt