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

function GameObject(gameDetails) {
this.createdAt = gameDetails.createdAt;
this.name = gameDetails.name;
this.dimensions = gameDetails.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(stats) {
this.healthPoints = stats.healthPoints;
GameObject.call(this, stats);
}

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,6 +54,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (humanoidAttributes) {
this.team = humanoidAttributes.team;
this.weapons = humanoidAttributes.weapons;
this.language = humanoidAttributes.language;
CharacterStats.call(this, humanoidAttributes);
}
Humanoid.prototype = Object.create(CharacterStats.prototype);

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



/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +77,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 +138,25 @@
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!

function Villain(villianAttributes){
Humanoid.call(this, villianAttributes);
}

Villian.prototype.finishHim = function(){
if (Hero.healthPoints <= 0) {
Hero.destroy();
}
}

function Hero(heroAttributes){
Humanoid.call(this, heroAttributes);
}

Hero.prototype = Object.create(Villian.prototype);
67 changes: 60 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
/* 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 binding

When in global scope, 'this' will always bind to the Window object. 'this' will always try to point to the Window in any place it appears.

* 2. Implicit binding

The way 'this' automatically binds to an object without being explicitly bound.
Implicit binding always refers to object methods. With implicit binding, we need to look at where
the method was called (not where it was created): the object left of the dot in a method is what 'this' binds to.

* 3. New binding

New binding comes into play with constructor functions (functions that create objects). The 'new' keyword that we use
when creating constructor functions automatically assigns a 'this' binding. In this case, the 'this' keyword
refers to the specific instance of the object that is created and returned by the constructor function.

* 4. Explicit binding

Explicit binding occurs when .call(), .apply(), or .bind() are used on a function. We intentionally bind this to an object.

* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
// In this example, the 'this' keyword has no context, so it binds to the Window object

function sayName(name) {
console.log(this);
return name;
}
sayName("Rasha");


// Principle 2

// code example for Implicit Binding

const user = {
name: 'Rasha',
age: 40,
greet() {
alert(`Hello, my name is ${this.name}`)
}
}

user.greet();



// Principle 3

// code example for New Binding

function User (name, age) {
this.name = name
this.age = age
}

const me = new User('Rasha', 40);

// Principle 4

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

function greet () {
alert(`Hello, my name is ${this.name}`)
}

const user = {
name: 'Rasha',
age: 40,
}

greet.call(user);