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,12 +16,33 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject (attributes) {
this.createdAt = attributes.createdAt,
this.name = attributes.name,
this.dimensions = attributes.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(attributes) {
GameObject.call(this, attributes);
this.name = attributes.name,
this.healthPoints = attributes.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,6 +53,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(attributes) {
CharacterStats.call(this, attributes);
this.name = attributes.name;
this.team = attributes.team,
this.weapons = attributes.weapons,
this.language = attributes.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 +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
40 changes: 35 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
/* 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 object binding - when logging 'this' in the global scope, it will return the global variables/functions for JavaScript.
* 2. Implicit binding - when a function is called with a dot in front of it, the object that is before the dot is refered to as 'this'.
* 3. New binding - when a constructor function is used, 'this' refers to where the object was made and returned by the constructor function.
* 4. Explicit binding - when .call or .apply method is used, this is clearly defined.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function myName(name) {
console.log(this);
return name;
}
myName("Ken");

// Principle 2

// code example for Implicit Binding
const introduce = {
greeting: 'My name is',
nameIntro: function (name) {
console.log(`${this.greeting} ${name}`);
console.log(this);
}
};
introduce.nameIntro('Ken');


// Principle 3

// code example for New Binding
function FavoriteAnimal (animal) {
this.intro = 'My favorite animal is a ',
this.animal = animal,
this.sentence = function() {
console.log(this.intro + this.animal);
console.log(this);
};
}

const animalShark = new FavoriteAnimal('shark');
const animalRhino = new FavoriteAnimal('rhino');

animalShark.sentence();
animalRhino.sentence();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
// animalShark.sentence.call(shark);
// animalRhino.sentence.apply(rhino);