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
37 changes: 34 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@
* 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(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 (attributes) {
GameObject.call(this, attributes);
this.healthPoints = attributes.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,7 +51,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid (attributes) {
CharacterStats.call(this, attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.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.
Expand All @@ -41,7 +73,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,7 +134,6 @@
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.
Expand Down
62 changes: 57 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* 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 - the scope of 'this' will refer to the top level window object
* 2. Implicit binding - the scope of 'this' will refer to the object using it
* 3. New binding - the scope of 'this' refers to the instance of the object that uses it
* 4. Explicit binding - the scope of 'this' refers to the object that used the call method to inherit the attribute 'this' is referring to
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,66 @@

// code example for Window Binding

function money (param) {
console.log(this);
return param;
}

money(5.02);

// Principle 2

// code example for Implicit Binding

const change = obj => {
obj.makeChange = function() {
console.log(this.name + " your change is " + (this.money - this.price));
};
};

const personOne = {name: 'Turtle', price: 1.52, money: 1.65};
const personTwo = {name: 'Snek', price: 5.03, money: 10};

change(personOne);
change(personTwo);

personOne.makeChange();
personTwo.makeChange();

// Principle 3

// code example for New Binding

function Dog(name) {
this.noise = "Bark";
this.name = name;
this.bark = () => {
console.log(`${this.name}: ${this.noise}`);
}
}

const fido = new Dog("Fido");
const rover = new Dog("Rover");

fido.bark();
rover.bark();

// Principle 4

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

function ThingOne (attributes) {
this.place = attributes.place;
};

function ThingTwo (attributes) {
ThingOne.call(this, attributes);
this.name = attributes.name;
};

const things = new ThingTwo({
name: 'person',
place: 'location'
});

console.log(things.place);
7 changes: 7 additions & 0 deletions js3.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "assignments"
}
]
}