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
272 changes: 200 additions & 72 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,40 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(gameAttr) {
this.createdAt = gameAttr.createdAt,
this.dimensions = gameAttr.dimensions,
this.name = gameAttr.name;
}

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(charAttr) {
this.healthPoints = charAttr.healthPoints,
GameObject.call(this, charAttr);
}

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

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

CharacterStats.prototype.speak = function () {
return `${this.name} barked precariously until completely passing out.`;
}


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,79 +59,180 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/



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

Humanoid.prototype = Object.create(CharacterStats.prototype); // inheritance

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



/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
* Instances of CharacterStats should have all of the same properties as GameObject.
*/
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
* Instances of CharacterStats should have all of the same properties as GameObject.
*/

// 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.
*/

// 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!
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',
});

// stretch

function HealhKnock(value) {
return value - Math.round(Math.random() * 100);
}


// villian
function Villian(villianAttr) {
Humanoid.call(this, villianAttr);
HealhKnock.call(this. villianAttr);
this.attack = villianAttr.attack;
this.hp = villianAttr.hp;
}

Villian.prototype = Object.create(Humanoid.prototype);


// hero
function Hero(heroAttr) {
Humanoid.call(this, heroAttr);
this.attack = heroAttr.attack;
this.hp = heroAttr.hp;
}

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




const Kai = new Villian({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 100,
name: 'Kai',
team: 'The Hot Dogs',
weapons: [
'Claw',
' Attitude',
],
language: 'Elvish',
attack: 'Claw with a pinch of attitude',
hp: 100,
});

const Eko = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 100,
name: 'Eko',
team: 'The Potatoes',
weapons: [
'Claw',
' Mouth',
],
language: 'Elvish',
attack: 'Steps on your feet',
hp: 100,
});



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.



console.log(`3`);
console.log(`2`);
console.log(`1`);
console.log(`Fight!`);
console.log(`${Kai.name} the Villian has ${Kai.healthPoints} healthpoints and the following weapons: ${Kai.weapons}!`);
console.log(`${Eko.name} the Hero has ${Eko.healthPoints} healthpoints and the following weapons: ${Eko.weapons}!`);
console.log(`Who will win?!`);
console.log(`${Kai.name} activates a special attack! (${Kai.attack})`);
console.log(`${Kai.name} does ${HealhKnock(Eko.hp)} damage to ${Eko.name}`);
console.log(`${Eko.name} retaliates back with a special attack! (${Eko.attack}), inducing ${HealhKnock(Kai.hp)} damage!`);



// 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!
51 changes: 43 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window Binding; the default, the window object in the browser. (global object)
* 2. Implicit Binding: when you're calling the function or invoking it (turning it on) the '.this' refers to the object or method left of the dot.
* 3. New Binding: You can construct a new object where .this points to it.
* 4. Explicit Binding: Refers to what you're PASSING INTO the argument. 'testing.call(test)'
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

// Principle 2
console.log(this);

// Principle 2
// code example for Implicit Binding

// Principle 3
let car = {
name: 'Mercedes',
model: 'AMG S 65 Sedan',
acceleration: 4.1,
runMe: function() {
console.log(`I drive a ${this.name} ${this.model} that accelerates in ${this.acceleration} seconds.`);
}
}

car.runMe();

// Principle 3
// code example for New Binding

function TheGreats(name) {
this.bark = " can bark!";
this.name = name;
this.speak = function() {
console.log(this.name + this.bark);
};
}

const kai = new TheGreats('Kai');
const eko = new TheGreats('Eko');

kai.speak();
eko.speak();


// Principle 4
// code example for Explicit Binding

function dog() {
console.log(`${this.name} the ${this.breed}`);
}

const myDog = {
name: 'Eko',
breed: 'Rhodesian Ridgeback'
}

// code example for Explicit Binding
dog.call(myDog);