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: 32 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,33 @@
* 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(attribute) {
this.createdAt = attribute.createdAt;
this.dimensions = attribute.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(attribute) {
gameObject.call(this,attribute )
this.healthPoints = attribute.healthPoints;
this.name = attribute.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.) ===
* team
Expand All @@ -32,7 +51,17 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attribute) {
characterStats.call(this, attribute);

this.team = attribute.team;
this.weapons = attribute.weapons;
this.language = attribute.language;
}
Humanoid.prototype = Object.create(characterStats.prototype);
Humanoid.prototype.greet = function () {
return `${this.name} Hello, Welcome to the round table? ${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 +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,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
51 changes: 46 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window Binding - This is kida like a default to the "this" keyword to the window or console object.
* 2. Implicit Binding - This is when the "this" keyword is use with dot notation to access properties on an object.
* 3. New Binding - This refers to using the "this" keyword for a specific instance of an object that is created and returned by the constructor function
* 4. Explicit Binding - This is used when using the call or apply method.
*
* write out a code example of each explanation above
*/

// Principle 1
function starTrek(name){
console.log(this);
return name;
}

starTrek("Captain kirk");

// code example for Window Binding

// Principle 2

const ussEnterprise = {
"greeting": "Welcome Captain Kirk",
"warpSpeed": function (string) {
console.log(`${this.greeting}. It is time! ${string}`);
console.log(this);
}
}

ussEnterprise.warpSpeed("For warp speed zulu!")
// code example for Implicit Binding

// Principle 3

// code example for New Binding
function firePhaser(attack){
this.command = "Fire"
this.attack = attack;
this.outcome = function () {
console.log(this.command + this.attack);
console.log(this);
};
}

const torpedo = new firePhaser("torpedo");
const phaser = new firePhaser("phaser");

// Principle 4

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

function firePhaser(attack) {
this.command = "Fire"
this.attack = attack;
this.outcome = function () {
return `prepare to ${this.command}`;

}
console.log(this.command + this.attack);
console.log(this);
}
const Kirk = new firePhaser("Status");
const Scotty = new firePhaser("Im not a doctor Jim");