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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Dominique
# JavaScript - III

This challenge focuses on using the `this` keyword as well as getting comfortable with prototypes by building out a fantasy themed video game.
Expand Down
73 changes: 69 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,28 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attr){
this.createdAt = attr.createdAt;
this.name = attr.name;
this.dimensions = attr.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(attr){
this.healthPoints = attr.healthPoints;
GameObject.call(this, attr);
}
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,7 +46,40 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(attr){
this.team = attr.team;
this.weapons = attr.weapons;
this.language = attr.language;
CharacterStats.call(this,attr);
}

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

function Villain(attr){
Humanoid.call(this, attr);
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.foo =function(){
return `${this.name} used ${this.weapons} and reduced his opponents ${this.healthPoints} by 5`;
}

function Hero(attr){
Humanoid.call(this, attr);
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.bar =function(){
return `${this.name} used ${this.weapons} and reduced his opponents ${this.healthPoints} by 10`;
}





/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +88,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 +139,22 @@
language: 'Elvish',
});

const villain = new Villain({
healthPoints: 10,
name: 'bowser',
weapons: [
'breath of fire'
]
});

const hero = new Hero({
healthPoints: 25,
name: 'yoshi',
weapons: [
'tongue of death'
]
});

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,8 +165,10 @@
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.foo());
console.log(hero.bar());
console.log(villain.destroy(hero));
// 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;
Expand Down
86 changes: 81 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* 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 : If you invoke a function that isnt specified by a 'this' then it will default to the window object

* 2. Implicit Binding : Is the most common rule and references when a function is invoked, look to the left of the dot and that should be what the 'this' keyword is referencing.

* 3. New Binding : You invoke this function with 'new' keyword the 'this' keyword inside that function is then bound to the object being constructed

* 4. Explicit Binding : Is telling the of call (pass in an argument one by one), apply (pass in an as an array), and bind (is like call but it returns a new function that you invoke later rather than immediately).
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +16,87 @@

// code example for Window Binding

const myName = function(){
console.log(this.name);
};

const me = {
name: 'Dominique'
};
myName();
window.name = 'Mac';
myName();

//cannot log my real name Dominique because i invoked it without a specified 'this'
//instead I set the window object to a name to 'Mac' and now can see how it grabs the window object instead




// Principle 2

// code example for Implicit Binding
const petImplicit = {
name: 'Mango',
breed: 'German Shorthair Pointer',
favFood: 'duck',
sayName: function(){
console.log(`My pet's name is ${this.name}`);
}
};
petImplicit.sayName(); //sayName is referecing the 'this' inside the pet function which is pet and on the left of the dot





// Principle 3

// code example for New Binding
const Animal = function(color, name, type){
this.color = color;
this.name = name;
this.type = type,
this.getInfo = function(){
console.log(`The new Animal info is as follows: ${this.color}, ${this.name}, ${this.type}`);
}
};
const puppy = new Animal('rhone', 'mango', 'GSP');
puppy.getInfo();



// Principle 4

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

// .call:
const eats = function(food1, food2, food3){
console.log(`${this.name}'s favorite thing to eat is ${this.favFood} but he also eats ${food1}, ${food2}, and ${food3}.`)
};

const petCall = {
name: 'Mango',
breed: 'German Shorthair Pointer',
favFood: 'duck'
};

eats.call(petCall); // eats is invoked and the this keyword is going to referece petCall
//we explicit say what the this keyword is on call
/*********************************** OR ************************/
const foods = ['treats', 'turkey', 'chicken'];

eats.call(petCall, foods[0], foods[1], foods[2]);
//with .call here i am passing a parameter in one at a time


//.apply
eats.apply(petCall, foods);
//does the exact same thing as .call but can now just pass in an array (i.e foods)

//.bind
const newFunction = eats.bind(petCall, foods[0], foods[1], foods[2]);
newFunction();
//same as .call but creates a new function that you can invoke later