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
74 changes: 71 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,85 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


/*
* 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.
*/

// Ignacio's Prototype Functions

// function GameObject(attributes){
// this.createdAt = attributes.createdAt,
// this.name = attributes.name,
// this.dimensions = attributes.dimensions
// }

// function CharacterStats(attributes){
// GameObject.call(this, attributes),
// this.healthPoints = attributes.healthPoints
// }

// function Humanoid(attributes){
// CharacterStats.call(this, attributes),
// this.team = attributes.team,
// this.weapons = attributes.weapons,
// this.language = attributes.language
// };

// Ignacio's Class Functions
class GameObject{
constructor(attributes){
this.createdAt = attributes.createdAt,
this.name = attributes.name,
this.dimensions = attributes.dimensions
}
destroy(){
return `${this.name} was removed from the game.`;
}
}

class CharacterStats extends GameObject{
constructor(attributes){
super(attributes);
this.healthPoints = attributes.healthPoints
}
takeDamage(){
return `${this.name} took damage.`;
}
}

class Humanoid extends CharacterStats{
constructor(attributes){
super(attributes);
this.team = attributes.team,
this.weapons = attributes.weapons,
this.language = attributes.language
}
greet(){
return `${this.name} offers a greeting in ${this.language}`;
}
}


// Humanoid.prototype.destroy = function() {
// return `${this.name} was removed from the game.`;
// }

// Humanoid.prototype.takeDamage = function(){
// return `${this.name} took damage.`;
// }

// Humanoid.prototype.greet = function(){
// return `${this.name} offers a greeting in ${this.language}`;
// }



// 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 All @@ -57,7 +126,7 @@
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,7 +171,6 @@
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
66 changes: 61 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window: It's when you invoque a function from the global scope, not inside any object
* 2. Implicit: It's when you invoque a function from inside an object's scope (you call the function or method with the object and a dot preceding)
* 3. New: it's when you invoque a constructor function. 'This' is refered to the object being created.
* 4. Explicit: it's used when there is inheritance, so you can override what object 'this' refers to.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,70 @@

// code example for Window Binding

let principle1 = {
name: 'Ignacio',
location: 'Peru',
class: 'WEB24',
greeting: function() {
return `Implicit: Hello Lambda, I'm ${this.name}`;
}
}
console.log(this);

// Principle 2

// code example for Implicit Binding

let principle2 = {
name: 'Ignacio',
location: 'Peru',
class: 'WEB24',
greeting: function() {
return `Implicit: Hello Lambda, I'm ${this.name}`;
}
}
console.log(principle2.greeting());

// Principle 3

// code example for New Binding

function Principle3(me) {
this.name = me.name,
this.location = me.location,
this.class = me.class
this.greeting = function() {
return `New: Hello Lambda, I'm ${this.name}`;
}
};

let ignacio = new Principle3({
name: 'Ignacio',
location: 'Peru',
class: 'WEB24',
});

console.log(ignacio.greeting());

// Principle 4

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

function Principle4(me) {
this.name = me.name,
this.location = me.location,
this.class = me.class
this.greeting = function() {
return `Explicit: Hello Lambda, I'm ${this.name}`;
}
};

// ignacio already declared in Principle 3

let rachel = new Principle4({
name: 'Rachel',
location: 'USA',
class: 'WEB34',
});

console.log(ignacio.greeting.call(rachel));