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

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(attrs) {
this.createdAt = attrs.createdAt;
this.name = attrs.name;
this.dimensions = attrs.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(stats) {
GameObject.call(this, stats);
this.healthPoints = stats.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,16 +50,28 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

// Test you work by un-commenting these 3 objects and the list of console logs below:
// Test your 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 +132,7 @@
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
47 changes: 36 additions & 11 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding: This in global scope refers to the window you are in. Outside of any functions.
* 2. Umplicit Binding: Whenever a function is called by a preceeding dot, the object before that dot is this. I am not sure how to reword this so I will just write a stream of consciousness for a second. When you call
a function from an object, if you say dog.speak(), then all the variables inside the function speak() that are preceeded with 'this.' (this.fur, this.tail, etc) they are referring to dog.
* 3. New Binding: When using a constructor, this refers to the property on the object being created.
* 4. Explicit Binding:
*
* write out a code example of each explanation above
*/

// Principle 1

//--Window/Global Object Binding
// code example for Window Binding

const video = {
title: 'interstellar',
play() {
console.log(this);
}
}

video.play();
// Principle 2

//--Implicit Binding
// code example for Implicit Binding

const video2 = {
title: 'interstellar',
play() {
console.log(this.title);
}
}

video2.play();
// Principle 3

//--New binding
// code example for New Binding
function Vcr(movie) {
this.title = movie,
this.play = function() {
console.log(this.title);
}
}

// Principle 4
const blockbuster = new Vcr('step brothers');

// code example for Explicit Binding
blockbuster.play();
// Principle 4
//--Explicit binding
// code example for Explicit Binding
blockbuster.play.call('the prestige'); //??????????????????????????????????????