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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
262 changes: 190 additions & 72 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,35 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

class GameObject {
constructor(gameAttributes) {
this.createdAt = gameAttributes.createdAt;
this.name = gameAttributes.name;
this.dimensions = gameAttributes.dimensions;
}
destroy() {
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
*/

class CharacterStats extends GameObject {
constructor(charAttributes) {
super(charAttributes);
this.healthPoints = charAttributes.healthPoints;
}
takeDamage() {
return `${this.name} took damage`;
}
}

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

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,79 +54,175 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


class Humanoid extends CharacterStats {
constructor(attributes) {
super(attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.language;
}
greet() {
return `${this.name} offers a greeting in ${this.language}`;
}
}

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

/*
* 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"
});

class Hero extends Humanoid {
constructor(heroAttributes) {
super(heroAttributes);
this.superPower = heroAttributes.superPower;
}
}

class Villain extends Humanoid {
constructor(villainAttributes) {
super(villainAttributes);
}
}

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

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

// 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!

const batman = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 5
},
healthPoints: 18,
name: "Batman",
team: "Arkham Knights",
weapons: ["Fists"],
language: "English"
});

const joker = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 15,
name: "The Joker",
team: "Arkham Villains",
weapons: ["Knife", "Gun"],
language: "English"
});

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.
// function Hero(heroAttributes) {
// Humanoid.call(this, heroAttributes);
// attack = function() {};
// }

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

// function Villain(villainAttributes) {
// Humanoid.call(this, villainAttributes);
// attack = function() {};
// }

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

// // * 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!

// const batman = new Hero({
// createdAt: new Date(),
// dimensions: {
// length: 1,
// width: 2,
// height: 5
// },
// healthPoints: 18,
// name: "Batman",
// team: "Arkham Knights",
// weapons: ["Fists"],
// language: "English"
// });

// 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!
// const joker = new Villain({
// createdAt: new Date(),
// dimensions: {
// length: 1,
// width: 2,
// height: 4
// },
// healthPoints: 15,
// name: "The Joker",
// team: "Arkham Villains",
// weapons: ["Knife", "Gun"],
// language: "English"
// });
54 changes: 49 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* 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 is the default in which the this keyword is bound to the window

* 2. With Implicit binding, the this keyword is bound to whatever is to the left of the dot

* 3. New binding is when a new function is created and the this keyword pulls info from another function into the new function

* 4. With explicit binding you can tell JavaScript to point to a certain value using call, apply, or bind


*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +18,53 @@

// code example for Window Binding

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

console.log(thisVariable());

// Principle 2

// code example for Implicit Binding

let Me = {
name: "Brandon",
age: 26,
sex: "Male",
speak: function() {
console.log(`${this.name} is a ${this.age} year old ${this.sex}.`);
}
};

Me.speak();

// Principle 3

// code example for New Binding

let Pet = function(name, age) {
this.name = name;
this.age = age;
};

let dog = new Pet("Jade", 3);
console.log(dog);
let cat = new Pet("Pete", 2);
console.log(cat);

// Principle 4

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

function Dog() {
console.log(this.sound);
}

let myDog = {
name: "Jade",
age: 3,
sound: "Woof"
};

Dog.call(myDog);