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
38 changes: 36 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(obj) {
this.createdAt = obj.createdAt;
this.name = obj.name;
this.dimensions = obj.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
*/

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

function CharacterStats(obj) {
GameObject.call(this, obj);
this.healthPoints = obj.healthPoints;
}

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,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

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

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

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +75,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 +136,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
45 changes: 36 additions & 9 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. Global Binding is when you use the 'this' keyword outside any scope(global scope) so it targets the window or console object.
* 2. Implicit Binding is when a method being called, the this would apply to the object on the left of the dots
* 3. New Binding is when a constructor function is being used and 'this' would target the specific object instance it's used on(at the time of invoke).
* 4. Explicit Binging is when a call, apply, or bind method is used in JavaScipt.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(`The following object is shown through an example of Window Binding`, this);

// Principle 2

// code example for Implicit Binding
const implicitBinding = {
property: "Test 1",
thisMethod: function() {
console.log(`The following object is shown through an example of Implicit Binding`, this);
}
};
implicitBinding.thisMethod();

// Principle 3

// code example for New Binding
function Binding(obj) {
this.name = obj.name;
this.property2 = obj.property2;
this.method1 = function() {
console.log(`The following object is shown through an example of ${this.name} Binding`, this);
}
}
const newBingingObject = new Binding({name: "New", property2: "new's Variable 2"});
newBingingObject.method1();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function explicitBinging(obj) {
Binding.call(this, obj); // new binding
this.property3 = obj.property3;
this.property4 = obj.property4;
}

const explicitBingingObject = new explicitBinging({
name: 'Explicit',
property2: 'explicit string 2',
property3: 'explicit string 3',
property4: 'explicit string 4'
});

explicitBingingObject.method1(); // calling method of object that was created from a constructor that used call