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
33 changes: 31 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* dimensions
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/
function GameObject(attributes) {
this.createdAt = attributes.createdAt;
this.dimensions = attributes.dimensions;
}
GameObject.prototype.destroy = function () {
return `${this.name} was removed from the game`;
}

/*
=== CharacterStats ===
Expand All @@ -22,6 +29,16 @@
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats (charStats){
GameObject.call(this, charStats);
this.hp = charStats.hp;
this.name = charStats.name;
}

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

/*
=== Humanoid ===
Expand All @@ -32,6 +49,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(humanoidAttributes){
CharacterStats.call(this, humanoidAttributes);
this.faction = humanoidAttributes.faction;
this.weapons = humanoidAttributes.weapons;
this.language = humanoidAttributes.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
Expand All @@ -41,7 +70,7 @@

// Test you work by uncommenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,7 +131,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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
48 changes: 43 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 - this will point to the window or console object in the global scope
* 2. Implicit Binding - The dot that precedes the function that is called points to the object before the dot.
* 3. Explicit Binding - when call or apply or used, this is explicited
* 4. New Binding - when using a function to create an object, the new keyword is used and this will refer to the specific instance of the newly created object.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,52 @@

// code example for Window Binding

function windowName(name) {
console.log(this);
return name;
};
windowName("hello");


// Principle 2

// code example for Implicit Binding
const hunter = {
traits: 'handsome',

introduceYourself: function(name) {
console.log(`My name is ${name}, and I am ${this.traits}.`)
}
}
hunter.introduceYourself('Hunter');


// Principle 3

// code example for New Binding

function BatmanAndRobin(dynamicDuo) {
this.greeting = 'I fight crime in a rubber suit, ';
this.dynamicDuo = dynamicDuo;
this.speak = function() {


console.log(this.greeting + this.dynamicDuo);
console.log(this);
};
}

const batman = new BatmanAndRobin('Robin');
const robin = new BatmanAndRobin('Batman');

console.log(batman);
console.log(robin);

batman.speak();
robin.speak();

// Principle 4

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

batman.speak.call(robin);