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
32 changes: 28 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

function GameObject(attributes){
this.createdAt = new Date();
this.dimensions = attributes.dimensions;
}
GameObject.prototype.destroy = function(){
return `${this.name} was removed from the game`;
}
/*
=== CharacterStats ===
* healthPoints
* name
* 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;
this.name = attributes.name;
}
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 +47,16 @@
* 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 +65,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 +126,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
30 changes: 23 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. when use this in the global element it points at the root element

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it points to window.

* 2. points at the variable inside the object
* 3. creating an object with an constructor
* 4. when you bind an argument to a function, the this will point at the argument
*
* write out a code example of each explanation above
*/

// Principle 1

this.alert('Help');
// code example for Window Binding

// Principle 2

// code example for Implicit Binding
const Max = {
name: 'Max',
age: 20,
greet: function() {
return `Hi, my name is ${this.name}`
}
}

// Principle 3

// code example for New Binding

function Car(name){
this.name = name;
}
const fiat = new Car('fiat');
// Principle 4

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

function callMe(){
console.log(`Please call ${this}`);
}
const callJames = callMe.bind('James');
console.log(callJames());