Skip to content
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
121 changes: 94 additions & 27 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,104 @@
/*
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 heirarchy.
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
heirarchy.

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 inherit from Humanoid. Use the objects at the bottom of the page to test your constructor functions.

Each constructor function has unique properites and methods that are defined in their block comments below:
At the bottom of this file are 3 objects that all inherit from
Humanoid. Use the objects at the bottom of the page to test
your constructor functions.

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

/*
=== GameObject ===
* createdAt
* dimensions
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
* destroy() // prototype method -> returns the string
'Object was removed from the game.'
*/

//Constructor Function for GameObject
function GameObject(gameattributes){
this.createdAt = gameattributes.createdAt;
this.dimensions = gameattributes.dimensions;
}

//Game Object Methods
GameObject.prototype.destroy = function (){
console.log(`Object was removed from the game.`)
}


/*
=== CharacterStats ===
* hp
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* takeDamage() // prototype method -> returns the string
'<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

//Constructor Function for for CharacterStats
function CharacterStats(charattributes){
GameObject.call(this, charattributes);
this.hp = charattributes.hp;
this.name = charattributes.name;
}

//inheriting properties from GameObject
CharacterStats.prototype = Object.create(GameObject.prototype);

//new methods for CharacterStats
CharacterStats.prototype.takeDamage = function(){
console.log(`${this.name} took damage`)
}

/*
=== Humanoid ===
* faction
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* greet() // prototype method -> returns the string '<object name>
offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


//Constructor Function for Humanoid
function Humanoid(humattributes){
CharacterStats.call(this,humattributes);
this.faction = humattributes.faction;
this.weapons = humattributes.weapons;
this.language = humattributes.language;
}

//inheriting properties from CharacterStats which inherits from Game Object
Humanoid.prototype = Object.create(CharacterStats.prototype);

//New methods for Humanoid
Humanoid.prototype.greet = function(){
console.log(`${this.name} offers a greeting in ${this.language}`)
}

/*
* Inheritance chain: Humanoid -> CharacterStats -> GameObject
* 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.

* 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 uncommenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -92,19 +149,29 @@
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
console.log(mage.createdAt); // Today's date

console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }

console.log(swordsman.hp); // 15

console.log(mage.name); // Bruce

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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!
// * Create two new objects, one a villian and one a hero and fight it out with methods!
92 changes: 87 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
/* 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 involves a scenario in which the 'this' keyword has no objects or functions to reference
other than the entire window (the all encompassing object where the code is being written)

* 2. Implicit Binding is a redirection of the 'this' keyword to a more contained environment such as an object.
it is implicit because the binding happens automatically within the object or function. e.g.

const myObject = {
'greeting': 'Hello',
'sayHello': function(name){
console.log(`${this.greeting} my name is: ${name}`);
//anything after this is ignored (return)
}
}

myObject.sayHello("Bob");

in the function above, the 'this' keyword refers to "myObject".

* 3. New Binding involves functions that create new objects. Use of this method would involve such a constructor function that
contains a 'this' statement and then a follow up that creates a new object with the same functionality of the originally created
constructor function.

function CordialPerson(greeter){
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function (){
console.log(`${this.greeting} ${this.greeter}`)
}
}

const jerry = new CordialPerson('Newman'); //new object
const newman = new CordialPerson('Jerry');

jerry.speak();
newman.speak();

A situation like the one above shows that 'this' originally references the CordialPerson 'greeter', but is then redirected to another
object that has been made with the constructor function.


* 4. Explicit Binding makes use of established methods that are attached to functions which are known as .call, .apply, and .bind.
Unlike with implicit binding, a method needs to be specified as an attachment to the function being invoked.


*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +53,56 @@

// code example for Window Binding

console.log(this);

// Principle 2

// code example for Implicit Binding

const myObject = {
'greeting': 'Hello',
'sayHello': function(name){
console.log(`${this.greeting} my name is: ${name}`);
//anything after this is ignored (return)
}
}

myObject.sayHello("Bob");

// Principle 3

// code example for New Binding

function CordialPerson(greeter){
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function (){
console.log(`${this.greeting} ${this.greeter}`)
}
}

const jerry = new CordialPerson('Newman'); //new object
const newman = new CordialPerson('Jerry');


jerry.speak();
newman.speak();

// Principle 4

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

const myObject = {
'name': 'Jim',
'age': 30
}

sayName.call(myObject);
sayName.call(myObject, ...skills) //this works;

// ... is called "spread syntax"
//.call is a built in method of functions that we can use explicitly pass in the this keyword

const skills = ['HTML','CSS','JS']

sayName.apply(myObject,skills)