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
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>JS III</title>
<title>JS III</title>

<script src="this.js"></script>
<script src="prototypes.js"></script>
Expand Down
33 changes: 30 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,33 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/
function GameObject(obj) {
this.createdAt = obj.createdAt;
this.dimensions = obj.dimensions;
}

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

// ===========================================================================================================================================
/*
=== CharacterStats ===
* healthPoints
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(stats) { // **************************** DO I KEEP USING attributes AS THE PARAMETER IN ALL CONSTRUCTORS?
this.healthPoints = stats.healthPoints;
this.name = stats.name;
}

CharacterStats.prototype.takeDamage = function() {
return `${this.GameObject} took damage.` // *********************** NOT SURE IF THIS IS RIGHT
}
// ****************************************************************** HOW DO I MAKE SURE IT INHERITS destroy() FROM GameObject's PROTOTYPE?
// ===========================================================================================================================================
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,7 +50,16 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(human) {
this.team = human.team;
this.weapons = human.weapons;
this.language = human.laguage;
}

Humanoid.prototype.greet = function() {
return `${this.GameObject} 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 +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
43 changes: 35 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
/* 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 Binding = the context of 'this' when it's not tied to anything. It's context is the global scope and in the browser that's the window
* 2. Implicit Binding = the context of 'this' when it's tied to a specific object using dot notation. 'This' refers to the specific object it's tied to (left of the dot in the invocation).
* 3. New Binding = the context of 'this' when it's tied to a new object created off a constructor function.
* 4. Explicit Binding = the context of 'this' when we override the object it's attached to with dot notation and give it a new object with .call/.apply/.bind
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this)

// Principle 2

// code example for Implicit Binding
const myObj = {
name: "Matt",
speak: function() {
return `My name is ${this.name}.`
}
};

// Principle 3
console.log(myObj.speak());

// Principle 3
// code example for New Binding
function Person(obj) {
this.name = obj.name,
this.age = obj.age,
this.speak = function() {
return `I am ${this.age} years old and my name is ${this.name}!`
}
};

const corey = new Person({
name: 'Corey',
age: 31
})

const paige = new Person({
name: 'Paige',
age: 22
})

console.log(corey.speak());
console.log(paige.speak());

// Principle 4
// code example for Explicit Binding

// code example for Explicit Binding
console.log(paige.speak.call(corey));