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

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

/*
=== GameObject ===
* createdAt
* name
* 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(gameAttrs) {
this.createdAt = gameAttrs.createdAt;
this.name = gameAttrs.name;
this.dimensions = gameAttrs.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(charStatAttrs) {
GameObject.call(this, charStatAttrs);
this.healthPoints = charStatAttrs.healthPoints;
}

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.) ===
Expand All @@ -32,79 +51,148 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

Humanoid.prototype = Object.create(CharacterStats.prototype);
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.
*/
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!

function Villain(villAttrs) {
console.log(villAttrs);
Humanoid.call(this, villAttrs);
}

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

Villain.prototype.removeHealthPoints = function(target) {
--target.healthPoints;
if (target.healthPoints > 0) {
return console.log(
`${target.name} your health points just dropped to ${
target.healthPoints
}!`
);
}
return console.log(
`${target.name} is dead his health points dropped to ${
target.healthPoints
}!`
);
};

function Hero(heroAttrs) {
Villain.call(this, heroAttrs);
}

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

const villain1 = new Villain({
name: 'superVillain',
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 8,
team: 'Villains'
});

const hero1 = new Hero({
name: 'myHero',
dimensions: {
length: 2,
width: 2,
height: 4
},
healthPoints: 10,
team: 'Avengers'
});

// 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!
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
villain1.removeHealthPoints(hero1);
hero1.removeHealthPoints(villain1);
66 changes: 58 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,76 @@
/* 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 :
When the this keyword points towards the global excecution context it refers to the window object.

* 2. Implicit Binding:
Are for Objects Literals. To know what the this keyword applies to we need to look at where the method was invocked!
(Looking to the left of the method we will find out to object the this keyword refers to in the method).

* 3. New Binding: Allows to create a "blueprint" of an object in a function constructor. By binding the an object to the function constructor,
with the "new" keyword the new object will get the properties that are defined within the constructor function.

* 4. Explicit Binding:
Are for functions. And can be used with 3 methods : .apply() .call() & .bind().
*
* write out a code example of each explanation above
*/

// Principle 1

console.log(`\n === Principle 1 ===`);
// code example for Window Binding
console.log(this);

//
// Principle 2

console.log(`\n === Principle 2 ===`);
// code example for Implicit Binding
const student = {
name: 'Steven',
school: 'Lambda School',
course: 'Full Stack Web',
introduce: function() {
console.log(
`Hi! My name is ${this.name} I am currently enrolled in the ${
this.course
} course from ${this.school}.`
);
}
};

// Principle 3
student.introduce();

//
// Principle 3
console.log(`\n === Principle 3 ===`);
// code example for New Binding
function Teacher(attrs) {
console.log(attrs);
this.name = attrs.name;
}

const teacher = new Teacher({
name: 'Wes Bos'
});

console.log(teacher);

//
// Principle 4
console.log(`\n === Principle 4 ===`);
// code example for Explicit Binding
const fruit = {
name: 'Pineapple',
destination: 'Amsterdam'
};

function shipped() {
console.log(
`The ${this.name.toLowerCase()}s are getting shipped to ${
this.destination
}.`
);
}

// code example for Explicit Binding
shipped.call(fruit);