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
190 changes: 105 additions & 85 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,53 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
* name
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that 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
*/

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
function GameObject(attr) {
this.createdAt = attr.createdAt,
this.name = attr.name,
this.dimensions = attr.dimensions
}

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

function CharacterStats(attr) {
// this calls GameObject's attributes and methods into CharacterStats for use.
GameObject.call(this, attr);

this.healthPoints = attr.healthPoints;

}

//Here, we use the Object.create() method to make the CharacterStats prototype inherit GameObject's prototype (which includes attributes and methods).
CharacterStats.prototype = Object.create(GameObject.prototype);

//takeDamage method prototype for CharacterStats
CharacterStats.prototype.takeDamage = function () {
return `${this.name} took damage.`
}

function Humanoid(attr) {
CharacterStats.call(this, attr);
this.team = attr.team,
this.weapons = attr.weapons,
this.language = attr.language
}
// using Object.create() to make Humanoid inherit CharacterStats' methods and attributes.
Humanoid.prototype = Object.create(CharacterStats.prototype);

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

/* prototype method ->
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,68 +62,67 @@

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

/*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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.
*/
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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
56 changes: 47 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
/* The for principles of "this";
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window Binding is the default for 'this' if none of the other Binding rules are being applied, UNLESS you are in Strict mode, in which case it will be undefined.
* 2. Implicit Binding is a very common rule. When the function is invoked, 'this' simply references to whatever object the method belongs to.
* 3. New Binding uses the 'new' keyword to construct a new object and point 'this' to the new object.
* 4. Explicit Binding tells 'this' EXACTLY what it belongs to.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

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

var achoo = 'ACHOOO!';

sneeze();

// Principle 2
// code example for Implicit Binding

// Principle 3
let angryApe = {
noise: 'GRAOOOOOORRRR!',
roar: function () {
console.log(this.noise);
}
}

angryApe.roar();

// Principle 3
// code example for New Binding

function Announcer(param) {
this.message = param;
}

let impactWarner = new Announcer('FIVE SECONDS UNTIL IMPACT.');

console.log(impactWarner.message);

// Principle 4
// code example for Explicit Binding

function strike() {
console.log(`${this.item}: ${this.hit}`);
}

let weaponOne = {
item: 'Sword',
hit: '*CLANG!*'
}

let weaponTwo = {
item: 'Club',
hit: '*THUNK!*'
}

// code example for Explicit Binding
strike.call(weaponOne);
strike.call(weaponTwo);