Skip to content
Merged
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
148 changes: 145 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

// function Parent(attributes) {
// this.age = attributes.age;
// this.location = attributes.location;
// this.name = attributes.name;
// this.phrase = attributes.phrase;
// }

const GameObject = function(attributes) {
this.createdAt = attributes.createdAt;
this.dimensions = attributes.dimensions;
}

GameObject.prototype.destroy = function() {
return "Object was removed from the game.";
}

/*
=== CharacterStats ===
* healthPoints
Expand All @@ -23,6 +39,18 @@
* should inherit destroy() from GameObject's prototype
*/

const CharacterStats = function(attributes) {
GameObject.call(this, attributes);
this.healthPoints = attributes.healthPoints;
this.name = attributes.name;
}

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 +60,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

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

console.log(Humanoid);

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +83,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 @@ -102,9 +144,109 @@
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!


//Hero boi
const Hero = function(attributes) {
Humanoid.call(this, attributes);
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attack = function(target) {
if(this.healthPoints > 0) {
if(target.healthPoints > 0) {
let damage = (Math.round(Math.random() * 10));
target.healthPoints = target.healthPoints - damage;
if (target.healthPoints > 0) {
console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`)
} else {
console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the destroy and takeDamage functions you already have for these

}
} else {
console.log(`${this.name} kicked ${target.name}'s dead body`)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

}
} else {
console.log(`${this.name}'s body twitched`)
}
}

//Villain boi
const Villain = function(attributes) {
Humanoid.call(this, attributes);
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.attack = function(target) {
if(this.healthPoints > 0) {
if(target.healthPoints > 0) {
let damage = (Math.round(Math.random() * 10));
target.healthPoints = target.healthPoints - damage;
if (target.healthPoints > 0) {
console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`)
} else {
console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`)
}
} else {
console.log(`${this.name} kicked ${target.name}'s dead body`)
}
} else {
console.log(`${this.name}'s body twitched`)
}
}






const aragorn = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
healthPoints: 20,
name: 'Aragorn',
team: 'Ranger',
weapons: [
'Andúril',
],
language: 'The laguage of man',
});

const lurtz = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
healthPoints: 20,
name: 'Lurtz',
team: 'Uruk-hai',
weapons: [
'Bow',
],
language: 'black speech',
});

lurtz.attack(aragorn);
aragorn.attack(lurtz);
lurtz.attack(aragorn);
aragorn.attack(lurtz);
lurtz.attack(aragorn);
aragorn.attack(lurtz);
lurtz.attack(aragorn);
aragorn.attack(lurtz);
lurtz.attack(aragorn);
aragorn.attack(lurtz);
lurtz.attack(aragorn);
aragorn.attack(lurtz);

66 changes: 61 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/* 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 describes a trait of this where if you dont say where you want this to
go it will automtically revert to the window or the global scope depending on whether or
not it is pointing at anything

* 2. implicit binding deals with objects and allows this to reach into it's parent object to
find the vale of what it's referencing.

* 3. explicit binding is similar to implicit in that it is grabbing something within itself
except that it is grabbing paramaters passed into the function it exists in instead og keys in
its object

* 4. new binding refers to using the this keyword in object constructors and it's used to
create key value pairs within the function that can be assigned to new objects based on the
parameters passed into the function
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +23,60 @@

// code example for Window Binding

const name = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Window Binding is just using this without binding it first.

firstName: 'Jacob',
lastName: 'Angulo'
}

console.log(name.firstName + ' ' + name.lastName)

// Principle 2

// code example for Implicit Binding

const me = {
firstName: 'Jacob',
lastName: 'Angulo',
speak: function() {
console.log(this.firstName + ' ' + this.lastName)
}
}

me.speak();

// Principle 3

// code example for New Binding

const NameConstuctor = function(param) {
this.firstName = param.firstName;
this.lastName = param.lastName
}

NameConstuctor.prototype.consoleFullName = function() {
console.log(`${this.firstName} ${this.lastName}`)
}

const miNombre = new NameConstuctor({
firstName: 'Jacob',
lastName: 'Angulo'
});

miNombre.consoleFullName();

// Principle 4

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

function sayName() {
console.log(this.firstName + ' ' + this.lastName)
}

const mePt2 = {
firstName: 'Jacob',
lastName: 'Angulo'
};

const myNameAgain = sayName.bind(mePt2);

myNameAgain();