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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ This challenge focuses on using the `this` keyword as well as getting comfortabl

**Follow these steps to set up and work on your project:**

* [ ] Create a forked copy of this project.
* [ ] Add your project manager as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.
* [x] Create a forked copy of this project.
* [x] Add your project manager as collaborator on Github.
* [x] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [x] Create a new branch: git checkout -b `<firstName-lastName>`.
* [x] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [x] Push commits: git push origin `<firstName-lastName>`.

**Follow these steps for completing your project.**

Expand Down
44 changes: 42 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,37 @@
* 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(attributes){
//I NEED TO CALL THE PARENTS IN THE CHILDREN
GameObject.call(this, attributes);
this.healthPoints = attributes.healthPoints;
}

CharacterStats.prototype = Object.create(GameObject.prototype)
//Upon failed attempts to find what it needs CharacterStats defaults to GameObject.

CharacterStats.prototype.takeDamage = function (){
return `${this.name} took damage.`;
}


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -33,6 +57,22 @@
* should inherit takeDamage() from CharacterStats
*/

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

Humanoid.prototype = Object.create(CharacterStats.prototype);

Humanoid.prototype.greet = function (){
return `${this.name} offers a greeting in ${this.language}`
};


//don't forget to use Object.create again here.. I think

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +81,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 +142,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
55 changes: 50 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,71 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Implict means look to the left of the dot when the function is CALLED not when it is created to see what 'this' is referring to.
* 2. Explicit means you're telling the invokation where to look by passing the variable of the object in as an arguement.
* 3. New Binding means it creates a new function that is not used/invoked immediatly rather it is saved for use in the future.
* 4. Window Binding is not something we want but if none of the others apply it will bind to the window/global window.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let number = {
faveNum: 14,
speak: function(){
console.log(`My favorite number is ${this.faveNum}`);
}
}
//console.log(this);
//speak(); //doesn't know where to look, not sure if this is binded to the window

// Principle 2

// code example for Implicit Binding

let Person = {
name: "Lindsey",
age: "Forever 21",
location: "NOLA",
greeting: function(){
console.log(`Hi! My name is ${this.name}, I am ${this.age} and I am from ${this.location}!`);
}
}
Person.greeting(); //this will look inside person to see what to use for 'this' in my console.log.

// Principle 3

// code example for New Binding

function newPerson(){
console.log(`My favorite person's name is ${this.name}`);
}

let myNewPerson = {
name: 'Sebastian',
age: 5,
gender: 'M',
}

favoritePerson = newPerson.bind(myNewPerson); //favoritePerson is the new function
favoritePerson(); //It's called down here, later or any time later. Not invoked initially.




// Principle 4

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

let myPet = {
name: 'Sandy',
age: 5,
type: 'dog',
markings: 'freckles on her nose',
}
function info(){
console.log(`My ${this.type} ${this.name} is ${this.age} years old and has ${this.markings}!`)
}
info.call(myPet); //*notes* call is a property on every function and the first argument you pass to it will be the context (or the focal object) in which the function is invoked. In other words, the first argument you pass to call will be what the this keyword inside that function is referencing.