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

function GameObject (chara) {
this.createdAt = chara.createdAt,
this.name = chara.name,
this.dimensions = chara.dimensions

};


GameObject.prototype.destroy = function(){
returns `${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 (stat) {
this.healthPoints = stat.healthPoints;

}

CharacterStats.prototype.takeDamage = function(obj){
return `${this.obj} took damage`;
};




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

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -33,6 +61,19 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(hum) {
this.team = hum.team,
this.weapons = hum.weapons,
this.language = hum.language
};

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

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

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +82,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 +143,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
52 changes: 46 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. 'This' refers to the window/console object when in global scope.
* 2. 'This' referes to the object to the left of the dot when binding is implicit.
* 3. 'This' referes to the specific instance of the object created and returned by a constructor function. This type of binding is accomplished using the New keyword.
* 4. 'This' is explicitly define when the in-built methods call or apply are used.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

function whatsThis() {
console.log(this);
};
// Principle 2

// code example for Implicit Binding

const thisObj = {
implicit: function(adj) {
console.log(`The ${adj} is real`);
console.log(this);

}


};

thisObj.implicit('struggle');
// Principle 3

// code example for New Binding
function Person (){
this.height = 'tall',
this.weight = 'healthy',
this.isHealthy = function(name) {
console.log(`${name} is ${this.height} and a ${this.weight} weight.`);
console.log(this);
};
}

const newPerson = new Person();
newPerson.isHealthy('Alex');

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function Person (){
this.height = 'tall',
this.weight = 'healthy',
this.isHealthy = function(name) {
console.log(`${name} is ${this.height} and a ${this.weight} weight.`);
console.log(this);
};
}

const newPerson = new Person();
newPerson.isHealthy('Alex');

const anotherPerson = new Person;
anotherPerson.isHealthy('Tom');

anotherPerson.isHealthy.call(newPerson);