Skip to content

initial commit #764

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 2 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
53 changes: 44 additions & 9 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
/*
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.

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.

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.
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.`
* destroy() // prototype method that returns: '{this.name} was removed from the game.'
*/

function GameObject(attr){
this.createdAt = attr.createdAt;
this.name = attr.name;
this.dimensions = attr.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.healthPoints = stats.healthPoints;
this.name = stats.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,6 +54,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(about){
CharacterStats.call(this, about);
this.team = about.team;
this.weapons = about.weapons;
this.language = about.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
Expand All @@ -41,7 +76,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 +137,7 @@
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
56 changes: 44 additions & 12 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
*/

// Principle 1
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window - works in global scope, "this" refers to window or console object.

* 2. Implicit - only communicates inside of the function/object that it lives in.
* "this" points to what comes to the left of the dot.

* 3. New - works with constructor functions, constructors always have a capital letter.
"this" refers to the new object created.

* 4. Explicit - "this" is defined when the call or apply method is used.
*
* write out a code example of each explanation above
*/


// Principle 1

// code example for Window Binding

console.log(this); //"this" refers to the global scope


// Principle 2

// code example for Implicit Binding
const jessNOV = {
name: 'Jess',
color: 'orange',
food: 'Thanksgiving dinner',
favoriteColor: function() {
console.log(`${this.name}'s favorite color is ${this.color}.`)
}
}

jessNOV.favoriteColor(); //"this" refers to name and color (left of the dots)

// Principle 3

// code example for New Binding
function Person(name, color) {
this.name = name;
this.color = color;
}

const jane = new Person('jane doe', 'yellow');

console.log(jane); //"this" refers to Person

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
let favoriteFood = function() {
console.log(`${this.name}'s favorite food is ${this.food}.`)
}

favoriteFood.call(jessNOV); //"this" refers to jessNOV