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
119 changes: 117 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@
* 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
*/

function CharacterStats(obj) {
GameObject.call(this, obj);
this.healthPoints = obj.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.) ===
* team
Expand All @@ -32,6 +52,61 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

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

/*
=== Villain (An evil son of a gun.) ===
* powerLevel
* powerUp() increase powerLevel property by 5 //
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
* should inherit greet() from Humanoid
*/

function Villain(obj) {
Humanoid.call(this, obj);
this.powerLevel = obj.powerLevel;
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.powerUp = function() {
this.powerLevel += 5;
return `${this.name} increased their power level by 5 points.`;
};

/*
=== Hero (A courageous crusader.) ===
* attack() decrease a chosen opponent's healhtPoints by 5, killing them if their HP reaches 0 //
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
* should inherit greet() from Humanoid
*/

function Hero(obj) {
Humanoid.call(this, obj);
this.powerLevel = obj.powerLevel;
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attack = function(victim) {
victim.healthPoints -= 5;
if (victim.healthPoints <= 0) {
return `Our hero attacked ${victim.name} and killed them!`;
} else {
return `Our hero attacked ${victim.name} and reduced their healthPoints by 5 points, bringing their remaining HP to ${victim.healthPoints}.`;
}
};

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +116,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 @@ -92,6 +167,41 @@
language: 'Elvish',
});

const villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 6,
},
healthPoints: 10,
name: 'Belic',
team: 'Alistain',
weapons: [
'Bow',
'Dagger',
],
language: 'Grunts and whistles',
powerLevel: 10
});

const hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 4,
width: 9,
height: 2,
},
healthPoints: 10,
name: 'Bob',
team: 'Jones',
weapons: [
'Buster Sword',
'Bolt Materia',
],
language: 'mute'
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -102,7 +212,12 @@
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.
*/
console.log(villain.powerLevel); // Belic shows off his power level.
console.log(villain.powerUp()); // Belic powers up.
console.log(villain.powerLevel); // Belic shows off his raised power level.
console.log(hero.attack(villain)); // Belic shows off his raised power level.
console.log(hero.attack(villain)); // Belic shows off his raised power level.


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
54 changes: 49 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window: When you are in the global scope, `this` is equal to the Window object. Examples include outside of any function, or inside a function that is not a method on an object. It also includes inside a function that is inside a method, so long as that function doesn't use arrow syntax.

* 2. Implicit: Most common default behavior. When calling a method, it goes by the *to the left of the dot* rule, where `this` refers to the object that the method resides on.

* 3. New: The new keyword does some things behind the scenes so you don't have to do it manually. When a constructor function is called with the `New` keyowrd, it creates an empty object, sets the `this` context equal to it, and returns the object. So for any method on the object, or on the constructor function's prototype, the `this` context will be referring to the object you just created.

* 4. Explicit: Manually setting the `this` context to whatever you want. You can run a method on one object, but have it use the `this` context of another object, so you can utilize functionality that one of your objects wouldn't ordinarily have access to. Call and apply work the same but apply takes arguments as an array. You can also use bind which does the same thing, but returns a new function which you can save and later call.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
var myObj = {
outerFunc: function() {
function innerFunc() {
console.log('I log the window object:', this);
}

innerFunc();
}
}

myObj.outerFunc();

// Principle 2

// code example for Implicit Binding

var myObjTwo = {
outerFuncTwo: function() {
console.log('I log the myObjTwo object:', this);
}
}

myObjTwo.outerFuncTwo();

// Principle 3

// code example for New Binding

function Cat(name, age, breed) {
this.name = name;
this.age = age;
this.breed = breed;
}

Cat.prototype.meow = function() {
console.log('My this keyword changes based on which object is created with the Cat function. In this case it refers to:', this.name);
}

var charlie = new Cat('Charlie', 17, 'Siamese');
var phoebe = new Cat('Phoebe', 12, 'Maine Coon');

charlie.meow();
phoebe.meow();

// Principle 4

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

var charlieImitatingPhoebe = charlie.meow.bind(phoebe);

console.log('** ---Charlie speaking--- **');
charlieImitatingPhoebe();
console.log('** ---Charlie speaking--- **');