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
36 changes: 33 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@
* 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(gameObj) {
this.createdAt = gameObj.createdAt;
this.name = gameObj.name;
this.dimensions = gameObj.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(char) {
GameObject.call(this, char);
this.healthPoints = char.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.) ===
Expand All @@ -32,7 +51,18 @@
* 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() {
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 +71,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 +132,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
31 changes: 26 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Implicit binding means that this refers to whatever comes before the . in calling a function, so if I had an object called "Bonnie" and invoked a function within it using this, this would refer to the object ie this.name would be the name listed in the object called "Bonnie".
* 2. Explicit binding works with .call .apply and .bind to tell the function what variable the this keyword means
* 3. New binding means that the this keyword inside a constructor object is used to create a new object when the constructor is called with the new keyword - this then refers to the new object instead of the constructor object
* 4. Window binding makes the this keyword refer the window or global scope
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this); //this refers to the window

// Principle 2

// code example for Implicit Binding
let hello = {
name: Bonnie,
sayHello: function() {
console.log(`Hello ${this.name}.`) //this refers to the variable 'hello'
}
}

// Principle 3

// code example for New Binding
function User(atts) {
this.name = atts.name,
this.email = atts.email,
this.hello = function() {
console.log(`Hello, my name is ${this.name}. My email address is ${this.email}`); //this refers to the new variable we create with the User constructor
};
};
const john = new User({
name: 'John',
email: 'test@test.com'
});
john.hello(); //for this example this would refer to john

// Principle 4
// .call - for comma seperated arguments, / apply - for array of arguments, / .bind for

// code example for Explicit Binding

// code example for Explicit Binding