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

mvp #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

mvp #762

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
65 changes: 45 additions & 20 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
/*
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(attributes){
this.createdAt = attributes.createdAt;
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(statsAttributes){
GameObject.call(this, statsAttributes);
this.hp = statsAttributes.hp;
this.name = statsAttributes.name;
}
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,30 +43,45 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(humanRobot){
GameObject.call(this, humanRobot);
CharacterStats.call(this, humanRobot);
this.faction = humanRobot.faction;
this.weapons = humanRobot.weapons;
this.language = humanRobot.language;
}


Humanoid.prototype = Object.create(GameObject.prototype);
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.
* Instances of CharacterStats should have all of the same properties as GameObject.
*/


// Test you work by un-commenting these 3 objects and the list of console logs below:

/*
// /*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
hp: 5,
name: 'Bruce',
team: 'Mage Guild',
faction: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
language: 'Common Toungue',
});

const swordsman = new Humanoid({
Expand All @@ -65,14 +91,14 @@
width: 2,
height: 2,
},
healthPoints: 15,
hp: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
language: 'Common Toungue',
});

const archer = new Humanoid({
Expand All @@ -82,9 +108,9 @@
width: 2,
height: 4,
},
healthPoints: 10,
hp: 10,
name: 'Lilith',
team: 'Forest Kingdom',
faction: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
Expand All @@ -94,17 +120,16 @@

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(swordsman.faction); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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!
62 changes: 55 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,74 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
1.Is the function called by new?
2.Is the function called by call(), apply(), or bind()?
3.is the function called as a method, ie: obj.func()?
4.Is the function called in the global scope?
//also
If strict mode is enabled, return undefined.
Otherwise, return the global object, ie: window.

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

// Principle 1

// code example for Window Binding

function name (newName){
console.log(this);
return newName;
}
name('Justin');

// Principle 2

// code example for Implicit Binding
//https://gist.github.com/zcaceres/2a4ac91f9f42ec0ef9cd0d18e4e71262

var MyObject = function (){
this.name = 'MyObjectName';
this.myProperty = 'property';
};

MyObject.prototype.doStuff = function (action) {
console.log(this.name + ' is ' + action + '!');
}

var obj = new MyObject();

obj.doStuff('awesome'); // prints 'MyObjectName is awesome!'
// Principle 3

// code example for New Binding

function Curb(akward){
this.akward = akward,
this.bad = `Pretty pretty pretty good`,
this.speak = function(){
console.log(`${this.akward} this is ${this.bad}`)
}
}

const Jones = new Curb('Jones');

larry.speak();

// Principle 4

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

const who = {
'name': 'Justin'
}

const work = [ 'manager', 'pharmacy', 'associate'];

const fun = ['xbox', 'swith', 'gym'];

function tellMeAbout (work1, work2, work3, fun1, fun2, fun3) {
console.log(`Hello, my name is ${this.name} and I worked as a ${work1}, ${work2}, and ${work3} and for fun i play: ${fun1}, ${fun2},
and go to the ${fun3}`)
}

tellMeAbout.call(who, ...work, ...fun); Binding