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
131 changes: 128 additions & 3 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(attrs) {
this.createdAt = attrs.createdAt;
this.name = attrs.name;
this. dimensions = attrs.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(attrs) {
GameObject.call(this, attrs);
this.healthPoints = attrs.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 @@ -39,9 +59,21 @@
* Instances of CharacterStats should have all of the same properties as GameObject.
*/

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

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

// 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,9 +134,102 @@
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.
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
// * Create two new objects, one a villain and one a hero and fight it out with methods!
// * Create two new objects, one a villain and one a hero and fight it out with methods!


function Hero(attrs) {
CharacterStats.call(this, attrs);
this.archnemesis = attrs.archnemesis
}

function Villain(attrs) {
CharacterStats.call(this, attrs);
this.archnemesis = attrs.archnemesis
}

Hero.prototype = Object.create(Humanoid.prototype);
Villain.prototype = Object.create(Humanoid.prototype);

function logAction(enemy) {
if (enemy.healthPoints > 5) {
return `Damage dealt! ${enemy.name}'s health is now: ${enemy.healthPoints -= 5}`
} {
alert(`Defeated! ${enemy.destroy()}`)
}
}

Hero.prototype.attackVillain = function(enemy) {
return function() {
return logAction(enemy)
}
}

Villain.prototype.attackHero = function(enemy) {
return function() {
return logAction(enemy)
}
}

const braveHero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 2,
},
healthPoints: 25,
name: 'Dre',
team: 'Brave Heros',
weapons: [
'War Hammer',
],
language: 'Common Tongue',
});

const evilVillain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
healthPoints: 25,
name: 'Hendrix',
team: 'Evil Villains',
weapons: [
'Axe',
],
language: 'Common Tongue',
});

let attackVillain = braveHero.attackVillain(evilVillain)
let attackHero = evilVillain.attackHero(braveHero)

function fight() {
if (evilVillain.healthPoints > 0) {
if (Math.floor(Math.random() * Math.floor(10)) > 5) {
console.log(`${braveHero.name} attacked:`, attackVillain());
}
}


if (braveHero.healthPoints > 0) {
if (Math.floor(Math.random() * Math.floor(10)) < 5) {
console.log(`${evilVillain.name} attacked:`, attackHero());
}
}

}

fight()
fight()
fight()
fight()
fight()
fight()
fight()
41 changes: 39 additions & 2 deletions assignments/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,54 @@
*/

// Principle 1
/* 'this' is bound to the global scope which is the Window/Global object */

// code example for Window Binding
function sayHello(greeting) {
console.log('Global: ', this);
console.log(`${greeting} world!`)
}

sayHello('Hello')

// Principle 2
/* 'When a method is called on an object, 'this' is bound to that object before the dot */

// code example for Implicit Binding
const myObj = {
name: 'Dre',
sayHello: function() {
console.log('Implicit: ', this);
console.log(`Hello ${this.name}`);
}
}

// Principle 3
myObj.sayHello()

// Principle 3
/* 'this' is bound to a specific instance of a object that was created by a constructor function */
// code example for New Binding
function Person(attrs) {
this.name = attrs.name
this.sayHello = function() {
console.log('New Binding: ', this);
console.log(`Hello ${this.name}`);
}
}

const me = new Person({
name: 'Dre',
})

me.sayHello()

// Principle 4
/* By using 'call' and 'apply', Explicit Binding let you overide what object 'this' refers to */

// code example for Explicit Binding
function sayHi() {
console.log('Explicit: ', this);
console.log(`Hi ${this.name}`);
}

// code example for Explicit Binding
sayHi.call(me)