Skip to content
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

finish JS3 #774

Open
wants to merge 1 commit 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
46 changes: 37 additions & 9 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/*
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
* name
* 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(att1){
this.createdAt = att1.createAt,
this.name = att1.name,
this.dimensions = att1.dimensions
}
GameObject.prototype.destroy = function () {
return `${this.name} was removed from the game.`;
}

/*
=== CharacterStats ===
Expand All @@ -23,6 +31,15 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(Att2){
this.healthPoints = Att2.healthPoints;
GameObject.call(this, Att2);
}
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 +49,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

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 +69,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 +130,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.

// 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!
44 changes: 39 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function x() {
console.log(this);

}

x();

// Principle 2

// code example for Implicit Binding

const person = {
firstName: 'john',
lastName: 'duo',
fun: function(){
console.log(this.firstName);
}
}

person.fun();

// Principle 3

// code example for New Binding

function person2(talk){
this.x = talk;
}

let newPerson = new person2('I am a new person');

console.log(newPerson.x); // function is being invoked


// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function any (){
console.log(this.firstName);
}
let newAny = {
firstName : 'Avi',
lastName : 'Kandinov'
}
something = any.bind(newAny);
something();