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
36 changes: 33 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,31 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attr) {
this.createdAt = attr.createdAt;
this.dimensions = attr.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(charObj) {
this.hp = charObj.hp;
this.name = charObj.name;
GameObject.call(this, charObj);
}
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.) ===
* team
Expand All @@ -33,6 +51,18 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(obj) {
this.faction = obj.faction;
this.weapons = obj.weapons;
this.language = obj.language;
GameObject.call(this, obj);
CharacterStats.call(this, obj);
}
Humanoid.prototype = Object.create(GameObject.prototype);
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 +71,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,9 +132,9 @@
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.
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
// * Create two new objects, one a villain and one a hero and fight it out with methods!
// * Create two new objects, one a villain and one a hero and fight it out with methods!
78 changes: 68 additions & 10 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,84 @@
/* The for principles of "this";
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Global/Window Object Binding:
When the this is called without anything left of the dot it is in the global object.
* 2. Implicit Binding:
when you call a function/method inside an object whatever comes before the dot is the this object.
* 3. New binding:
when using a Constructor the object that is created by the constructor is the this object
* 4. Explicit binding:
You can use .call, .apply or .bind to tell this what object it is applied to.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

function principal1() {
// The this below is refering to global object becasue it was not called with anyting to the left of the dot.
console.log(this);
}
// Principle 2

// code example for Implicit Binding
let principle2 ={
name : "john",
age: 25,
call: function(){
console.log(`Hi! my name is ${this.name} and my age is ${this.age}`);
console.log(this);
}
}
principle2.call();

// Principle 3
//Hi! my name is john and my age is 25
//[object Object]: {age: 25, call: function(){ console.log(`Hi! my name is ${this.name} and my age is ${this.age}`), console.log(this); }, name: "john"}
// Here we can see this refers to principle2 which is on the left side of the .

// Principle 3
// code example for New Binding
let Car= function(make,model,year){
return{
make: make,
model: model,
year: year,
message: function(){
console.log(`Hi, I am a ${this.year} ${this.make} ${this.model}`);
console.log(`In cases of constructor functions this refers to the newly formed object.`);
console.log(`In this case this refers to ${this.model}`);
console.log(`once again it refers to the object listed left of the dot.`);
}
}
}
let camry = Car('Toyota','Camry',1996);
camry.message();

// Hi, I am a 1996 Toyota Camry
// In cases of constructor functions this refers to the newly formed object.
// In this case this refers to Camry
// once again it refers to the object listed left of the dot.

// Principle 4
// code example for Explicit Binding

// Create Camry object
let camry ={
make: "Toyota",
model: "Camry",
year: 1996
}

// Create message prototype
let message = function(){
console.log(`Hi, I am a ${this.year} ${this.make} ${this.model}`);
console.log(`In cases of Explicit Binding you define what this refers to`);
console.log(`In this case this refers to ${this.model}`);
console.log(`Even though that is not the object to the left of the dot`);
}

message.call(camry);

// code example for Explicit Binding
// Hi, I am a 1996 Toyota Camry
// In cases of Explicit Binding you define what this refers to
// In this case this refers to Camry
// Even though that is not the object to the left of the dot