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

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


console.log("---------------Prototypes.js exercises----------------");

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +18,67 @@
* 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, */childAttributes) {
GameObject.call(this, childAttributes);
this.healthPoints = childAttributes.healthPoints;
this.isAlive = true;
}

CharacterStats.prototype = Object.create(GameObject.prototype);
// CharacterStats.prototype.constructor = CharacterStats; ?????

CharacterStats.prototype.destroy = function() {
console.log(`${this.name} was removed from the game.`);
this.isAlive = false;
}

CharacterStats.prototype.takeDamage = function(damage) {
// return '<object name> took damage.';
// return `${this.name} took damage.`;
this.healthPoints -= damage;
console.log(`${this.name} took ${damage} damage. (${this.healthPoints} health remaining)`);
// console.log(`(${this.healthPoints} health remaining), this.healthPoints <= 0 = ${this.healthPoints <= 0}`)
if(this.healthPoints <= 0)
this.destroy();
}

// var childAttributes = {
// createdAt: "2019",
// name: "chicken",
// dimensions: "40ft",
// healthPoints: "1"
// }

// const childAttributes = {
// healthPoints: "1"
// }

// var charStats = new CharacterStats(/*attributes, */childAttributes);

// console.log(charStats.destroy());
// console.log(charStats.dimensions);
// console.log(charStats.createdAt);
// console.log(charStats.healthPoints);
// console.log(charStats.takeDamage());

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -33,6 +89,36 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(childAttributes) {
CharacterStats.call(this, childAttributes);

this.team = childAttributes.team;
this.weapons = childAttributes.weapons;
this.language = childAttributes.language;
}

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

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


/*this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.dimensions; */

// var player1 = new Humanoid({
// team: "1",
// weapons: ["rock"],
// language: ["Orcish"],
// createdAt: "3000-3-17",
// dimensions: "2d",
// name: "xD"
// });

// console.log(player1.team, player1.weapons, player1.language, player1.name, player1.greet(), player1.takeDamage(), player1.destroy());

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

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

/*
const mage = new Humanoid({

/*const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
Expand Down Expand Up @@ -101,10 +187,119 @@
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(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 Humanoid(childAttributes) {
CharacterStats.call(this, childAttributes);

this.team = childAttributes.team;
this.weapons = childAttributes.weapons;
this.language = childAttributes.language;
}

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

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

*/

function Hero(childAttributes) {
Humanoid.call(this, childAttributes);
// this.isAlive = true;
}

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

// Hero.prototype.destroy = function() {
// console.log(`${this.name} was removed from the game.`);
// this.isAlive = false;
// }

// Hero.prototype.takeDamage = function(damage) {
// this.healthPoints -= damage;
// console.log(`hero take damage ${this.name} took ${damage} damage (${this.healthPoints} health remaining).`);
// if(this.healthPoints <= 0)
// this.destroy;
// }

Hero.prototype.shinKick = function(vill) {
console.log(`${this.name} used shinkick!`);
vill.takeDamage(Math.floor(Math.random() * 11));
}

function Villain(childAttributes) {
Humanoid.call(this, childAttributes);
// this.isAlive = false;
}

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

// Villain.prototype.destroy = function() {
// console.log(`${this.name} was removed from the game.`);
// // this.isAlive = false;
// }

// Villain.prototype.takeDamage = function(damage) {
// this.healthPoints -= damage;
// console.log(`villan take damage ${this.name} took ${damage} damage (${this.healthPoints} health remaining).`);
// if(this.healthPoints <= 0) {
// console.log("his.destroy;");
// this.destroy;
// }
// }

Villain.prototype.eyeJab = function(hero) {
console.log(`${this.name} used eyejab!`);
for(let i = 0; i < 3; i++)
hero.takeDamage(Math.floor(Math.random() * 4));
}

const sanic = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 3,
},
healthPoints: 50,
name: "sanic",
team: "team1",
weapons: [],
language: "boognish",
});

const eggDude = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 3,
},
healthPoints: 50,
name: "eggDude",
team: "team0",
weapons: [],
language: "portuguese",
});

while(sanic.isAlive && eggDude.isAlive) {
if(sanic.isAlive && eggDude.isAlive) { //seems redundant
sanic.shinKick(eggDude);
eggDude.eyeJab(sanic);
console.log("\n");
}
}

if(sanic.isAlive)
console.log("sanic wins!");
else
console.log("eggDude wins!");
42 changes: 37 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 1. Window binding refers to the window object and is the default if no other values for "this" are found.
* 2. Implicit binding means that "this" refers to the parent object from which the function is called.
* 3. New binding is where you create a new instance of an object to which "this" points.
* 4.
*
* write out a code example of each explanation above
*/

console.log("---------------Git.js-----------------");
// Principle 1

// code example for Window Binding

Window.weirdVar = "Probably shouldn't be making window properties";
console.log(Window.weirdVar);

// Principle 2

// code example for Implicit Binding

const foo1 = {
name: "foo1",
GetName: function() {
return this.name;
}
}
console.log(foo1.GetName());

// Principle 3

// code example for New Binding

function Animal(type) {
this.type = type;
}

const myAnimal = new Animal("platypus");

console.log (myAnimal.type);

// Principle 4

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

const foo2 = {
name: "foo2"
}

function GetName() {
return this.name;
}

console.log(GetName.call(foo2));

var newGetName = GetName.bind(foo1);
console.log(newGetName());