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

function GameObject(attributes){
this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.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(charAttributes) {
GameObject.call(this, charAttributes);
this.healthPoints = charAttributes.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,16 +52,53 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(humanAttributes) {
CharacterStats.call(this, humanAttributes);
this.team = humanAttributes.team;
this.weapons = humanAttributes.weapons;
this.language = humanAttributes.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.
*/

// Stretch task:

function Villain(villainAttributes) {
Humanoid.call(this, villainAttributes);
this.deception = villainAttributes.deception;
this.poison = villainAttributes.poison;
}

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

Villain.prototype.stabBack = function() {
return `${this.name} has just deceived you!`
}

function Hero (heroAttributes) {
Humanoid.call(this, heroAttributes);
this.truth = heroAttributes.truth;
this.strength = heroAttributes.strength;
}
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attack = function() {
return `${this.name} will win in the end.`
}

// 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 All @@ -56,6 +113,7 @@
'Staff of Shamalama',
],
language: 'Common Tongue',

});

const swordsman = new Humanoid({
Expand All @@ -73,6 +131,7 @@
'Shield',
],
language: 'Common Tongue',
deception: 'You have just been deceived.',
});

const archer = new Humanoid({
Expand All @@ -90,6 +149,23 @@
'Dagger',
],
language: 'Elvish',
deception: 'You have just been deceived.',
});

const Father = new Villain ({
name: 'Sir Mustachio',
deception: 'You have just been deceived.',
poison: -1,
truth: 'The truth will guide you',
strength: 20,
});

const Son = new Hero ({
name: 'Lilith',
deception: 'You have just been deceived.',
poison: -1,
truth: 'The truth will guide you',
strength: 20,
});

console.log(mage.createdAt); // Today's date
Expand All @@ -102,9 +178,12 @@
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!

console.log(Father.stabBack());
console.log(Son.attack());
71 changes: 58 additions & 13 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,71 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*

* 1. when in the global scope the value of this will be the window / console object.
* My words: displays JS window log

* 2. whenever a function is called by a preceding dot, the object left of the dot gets this.
* My words: tis = value of the object to the left of the "."

* 3. whenever a constructor function is used this refers to the specific instance of the object that is created and returned by the constructor function.
* My words: this = the function the obhect created

* 4. whenever JS call or apply method is used this is expicitly defined.
* My words: this = this within the object

* write out a code example of each explanation above
*/

// Principle 1

// Principle 1 -- Global Binding
// code example for Window Binding
function sayName(name) {
console.log(this);
return name;
}
sayName("Dannie");

// Principle 2

// Principle 2 -- Implicit Binding
// code example for Implicit Binding
const myObj = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
myObj.sayHello('Jane');

// Principle 3

// Principle 3 -- New Binding
// code example for New Binding
function CordialPerson(greeter) {
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + this.greeter);
console.log(this);
};
}
const jerry = new CordialPerson('Newman');
const newman = new CordialPerson('Jerry');

jerry.speak();
newman.speak();

// Principle 4
// code example for Explicit Binding
function CordialPerson(greeter) {
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + this.greeter);
console.log(this);
};
}
const jerry = new CordialPerson('Newman');
const newman = new CordialPerson('Jerry');

jerry.speak.call(newman);
newman.speak.apply(jerry);

// code example for Explicit Binding
newman.speak();
jerry.speak();