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
35 changes: 32 additions & 3 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:
*/

function GameObject(attributes){
this.createAt = attributes.createAt;
this.dimensions= attributes.dimensions;
}
GameObject.prototype.destroy = function(){
return "Object neutralized!";
};
/*
=== 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.`
*/


/*
=== 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.hp = charAttributes.hp;
this.name = charAttributes.name;
this.healthPoints = charAttributes.healthPoints;
}
CharacterStats.prototype = Object.create(GameObject.prototype);

CharacterStats.prototype.takeDamage = function(){
return `${this.name} has been hit.`;
}

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,6 +50,17 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(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} says Hello ${this.language}.`;

};

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +70,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 +131,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
38 changes: 33 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
/* 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.
* 2. Whenver a function is called by a preceding dot, the object before that dot is this.
* 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.
* 4. Whenever JavaScript's call or apply method is used, "this" is explicitly defined.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function sayName(name) {
console.log(this);
return name;
}
sayName("D'Artagnan");

// Principle 2

// 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('Ryan');

// Principle 3

// 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
// code example for Explicit Binding
jerry.speak.call(newman); newman.speak.apply(jerry);