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
70 changes: 67 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,37 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(GOattrs) {
this.createdAt = GOattrs.createdAt;
this.name = GOattrs.name;
this.dimensions = GOattrs.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(CSattrs) {
GameObject.call(this, CSattrs);
this.healthPoints = CSattrs.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.) ===
* team
Expand All @@ -32,6 +56,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(Hattrs){
CharacterStats.call(this, Hattrs);
this.team = Hattrs.team;
this.weapons = Hattrs.weapons;
this.language = Hattrs.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
Expand All @@ -41,7 +78,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 +139,36 @@
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!

function Villain(Vattrs){
Humanoid.call(this, Vattrs);
this.team = Vattrs.team;
this.weapons = Vattrs.weapons;
this.language = Vattrs.language;
}

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

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

function Hero(Heroattrs){
Humanoid.call(this, Heroattrs);
this.team = Heroattrs.team;
this.weapons = Heroattrs.weapons;
this.language = Heroattrs.language;
}

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

Hero.prototype.greet = function(){
return `${this.name} offers a greeting in ${this.language}.`;
}
50 changes: 45 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
/* 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: this will refer to the window/console Object;
* 2. Implicit Binding: this will refer to the object before the dot
* 3. New Binding: this will refer to the specific instance of the object that is created and returned by the constructor function
* 4. Explicit Binding: this is explicitly defined by call or apply method
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let myPet = function (){
console.log(this.age);
}

const me = {
age: 12
}

myPet();

// Principle 2

// code example for Implicit Binding

let yourPet = {
name: 'Snoopy',
greeting: function(sound){
console.log(`My ${this.name} is ${sound}.`)
}
};

yourPet.greeting('meowing')

// Principle 3

// code example for New Binding
function Pet(breed, name, color){
this.breed = breed;
this.name = name;
this.color = color;
this.say = function() {
console.log(`My ${this.breed}'s name is ${this.name} and he is ${this.color}`);
}
}

const Theo = new Pet('cat', 'Theo', 'white')
Theo.say();


// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
const sayName = function(){
console.log(`My name is ${this.name}`);
};

const Maya = {
name: "Maya"
};

sayName.call(Maya);