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
41 changes: 38 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@
* 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(attr){
this.createdAt = attr.createdAt,
this.name = attr.name,
this.dimensions = attr.dimensions
}
// Prototype Method. Moved out of GameObject function due to memory issues.
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(attr){
this.healthPoints = attr.healthPoints,
//For inheriting, you are calling this and parameter. Also, you need to create a child contructor.
// Calling GameObject's attributes
GameObject.call(this, attr)
}
// Creating child constructor.
// Created child constructor needs to go before all.
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 +53,30 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attr){
this.team = attr.team,
this.weapons = attr.weapons,
this.language = attr.language,
CharacterStats.call(this, attr)
}

Humanoid.prototype = Object.create(CharacterStats.prototype);
// Greet prototype method
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:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,7 +137,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
46 changes: 40 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
* in your own words. explain the four principles for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding: Whenever a function is contain in the globel scope, the value of "this" is equeal to the window object.

* 2. Implicit Binding: Whenever a function is called, the object to the left of the dot(.) is what "this" is referring to.

* 3. New Binding: Whenever a constructor function is used, the "this" keyword is referring to the specific instance of the object that is created and returned by my constructor function.

* 4. Explicit Binding: Whenever "call" or "apply" is used, "this" is explicitly defined.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function cartoonName(name){
console.log(this);
return name;
}
cartoonName("Doug");

// Principle 2

// code example for Implicit Binding
const dog = {
greeting: "woof!",
speak: function(dogsName){
console.log(`${dogsName} says "${this.greeting}"`);
}
}
dog.speak('Barky');

// Principle 3

// code example for New Binding
function sayHello(somebody){
this.greeting = "Hey";
this.janGreet = "Hiiii!";
this.somebody = somebody;
this.greet1 = function(){
console.log(`${somebody} says "${this.greeting}"`)
}
this.greet2 = function(){
console.log(`${somebody} says "${this.janGreet}"`)
}
}

const meg = new sayHello("Meg");
const jan = new sayHello("Jan");

meg.greet1();
jan.greet2();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
meg.greet1.call(jan);
jan.greet2.call(meg);