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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# JavaScript - III

hype
This challenge focuses on using the `this` keyword as well as getting comfortable with prototypes by building out a fantasy themed video game.

## Git Set up
Expand Down
126 changes: 116 additions & 10 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,38 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

function GameObject(property) {
this.createdAt = property.createdAt;
this.dimensions = property.dimensions;
/*this.destroy = function() {
return `${this.name} was removed from the game`
};
*/
};
GameObject.prototype.destroy = function() {
console.log( `${this.name} was removed from the game`);
};
/*
Comment thread
LarynaB marked this conversation as resolved.
Resolved
=== CharacterStats ===
* healthPoints
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(attributes) {
GameObject.call(this, attributes);
this.healthPoints = attributes.healthPoints;
this.name = attributes.name;
/*this.takeDamage = function() {
return `${this.name} took damage.`;
};
*/
};
CharacterStats.prototype = GameObject.prototype;

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

Comment thread
LarynaB marked this conversation as resolved.
Resolved
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,16 +56,60 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
function Humanoid(stats){
GameObject.call(this, stats);
CharacterStats.call(this, stats);
this.team = stats.team;
this.weapons = stats.weapons;
this.language = stats.language;
/* this.greet = function() {
return `${this.name} offers a greeting in ${this.language}`;
};
*/
};
Comment thread
LarynaB marked this conversation as resolved.
Resolved
Humanoid.prototype = CharacterStats.prototype;

Humanoid.prototype.greet = function () {
console.log( `${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.
*/

//Stretch Villain constructor
/*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;*/
function Villain(mod){
Humanoid.call(this, mod);
/*this.poisonDagger = function() {
return `${this.name} throws a deadly poisoned dagger! Damage for 10 health points.`;
};
*/
};
Villain.prototype = Humanoid.prototype;

Villain.prototype.poisonDagger = function () {
console.log( `${this.name} throws a deadly poisoned dagger! Damage for 10 health points.`);
};

function Hero(mod){
Humanoid.call(this, mod);
/*this.charmingEyes = function() {
return `${this.name} stares deeply into your eyes, you feel feverish and overwhelmed, weakened.`;
};
*/
};
Hero.prototype = Humanoid.prototype;

Hero.prototype.charmingEyes = function () {
console.log( `${this.name} stares deeply into your eyes, you feel feverish and overwhelmed, weakened.`);
};

// 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 @@ -91,6 +159,41 @@
],
language: 'Elvish',
});
// Stretch * Create two new objects, one a villain and one a hero and fight it out with methods!
const paul = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 3,
height: 5,
},
healthPoints: 5,
name: "Super Paul",
team: 'Sky Kingdom',
weapons: [
'Fists',
'Iron Muscles',
],
language: 'English'
});

const eric = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 3,
},
healthPoints: 7,
name: "Exilimor",
team: 'Nether Warp',
weapons: [
'Electron Whip',
'Plasma Flak Cannon',
],
language: 'Enrwiw'
});


console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
Expand All @@ -99,12 +202,15 @@
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.
*/
archer.greet(); // Lilith offers a greeting in Elvish.
mage.takeDamage(); // Bruce took damage.
swordsman.destroy(); // Sir Mustachio was removed from the game.
paul.charmingEyes(); // ${this.name} stares deeply into your eyes, you feel feverish and overwhelmed, weakened.
eric.poisonDagger(); // ${this.name} throws a deadly poisoned dagger! Damage for 10 health points.
paul.destroy(); // Super Paul was removed from game.

// Stretch task:
// Stretch task: Complete
// * 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!
// Complete
42 changes: 36 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Global(window) binding means that the value of "this" is on a global scope. Meaning one huge container like
window or console is operating as a global unit, everything applies to eachother.
* 2. Implicit Binding is where "this" will be assigned to the object left of dot notation. Object is "this".
* 3. New Binding is where within an Object the specific instance/attribute becomes "this"
and will be returned by the object.
* 4. Explicit is where "this" is defined through a call or apply method.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function sayQuack(sound) {
console.log(this);
return sound;
};
console.log(sayQuack("QUACK"));

// Principle 2

// code example for Implicit Binding

const myPen = {
pushButton: "(click click)",
holdingPen: function(name){
console.log(`${this.pushButton} Hi there user ${name}.`);
}
};
myPen.holdingPen("Alaj");
// Principle 3

// code example for New Binding
function WeaponA(weaponName){
this.weaponText = "Alpha Tier";
this.weaponName = weaponName;
this.rules = function () {
console.log(this.weaponText + this.weaponName)
};
console.log(this);
};
const swordOfAtlantis = new WeaponA(' Sword of Atlantis');
const axeOfEmbers = new WeaponA(' Axe of Embers');
const daggerObsidian = new WeaponA(' Obsidian Dagger');
swordOfAtlantis.rules();

// Principle 4

// code example for Explicit Binding

// code example for Explicit Binding
swordOfAtlantis.rules.call(axeOfEmbers);
axeOfEmbers.rules.apply(daggerObsidian);

swordOfAtlantis.rules();