Skip to content

Ndawi JavaScript-III #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
50 changes: 43 additions & 7 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,36 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attributes){
this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.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) {
GameObject.call(this, stats);
this.hp = stats.hp;
this.name = stats.name;
}
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function() { return `${this.name} has taken damage.`;
};


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,7 +55,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


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

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 Down Expand Up @@ -104,7 +140,7 @@
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.
// 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!
56 changes: 49 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
/* 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 - whenever a function is in the global scope, the value of this will be the window object. this is the default of all the "this" keywords

* 2. Implicit binding - when a function called beofre a dot, what is to the left of the dot points to the object on which the function is called

* 3. Explicit binding - There are 3 in total: call, bind, and apply. Call and apply are similar in that accomplish the same job, expect call is for singluar item, while apply is for arrays. bind is can store the information by creating a function so the data can be used again.

* 4. New binding - this keyword creates a new object and when the a function is called the this will reference to the object that will be created


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

// Principle 1


// Principle 1
// code example for Window Binding
function window(){
return console.log(this)
}
window();


// Principle 2

// code example for Implicit Binding
const myBaby= {
name: 'Lexi',
mess: function(){
console.log(`${this.name} smashes her brithday cake in her face`)
}
}

myBaby.mess();


// Principle 3

// code example for New Binding

function gymEmployee(trainer){
this.order= 'GIVE ME 20 PUSH UPS!';
this.trainer = trainer;
this.workout = function(){
console.log(this.order + this.trainer);
console.log(this);
}
}
const beth = new gymEmployee('Beth');
beth.workout();

// Principle 4

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

const tennisPlayer = {
name: 'Serena Williams'
};

const equipment = ['rackets', 'tennis balls', 'towels'];

function hey(equipment1, equipment2, equipment3){
console.log(`${this.name} responded "My match is about to begin, please bring my ${equipment1}, ${equipment2}, ${equipment3} to the court.`);
};

hey.apply(tennisPlayer, equipment);