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
47 changes: 45 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){

GameObject.call(this, attributes);

this.healthPoints = attributes.healthPoints;
}

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 @@ -33,6 +57,25 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attributes){

GameObject.call(this, attributes);

CharacterStats.call(this, attributes);

this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.language;

console.log(this);
}

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 +84,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 +145,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
88 changes: 80 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,98 @@
/* 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-If there isn't anything left of the dot then the this keyword defaults to the window.
* 2. Implicit Binding- You are putting a piece of information or string to the left of the dot.
* 3. New Binding- You are creating a new object and it is stored in the this keyword.
* 4. Explicit Binding- You are putting an argument inside the parenthesis of the console.log() using .call, .bind, or .apply.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function listName(){
console.log(`My name is ${this.name}`)
}

// Principle 2
const user = {
name: 'Elliot'
}

// Principle 2
// code example for Implicit Binding
let myBook = {
name: 'The Sword of Truth',
author: 'Terry Goodkind',
genre: 'Fantasy',
book: function() {
console.log(this.name);
}
}

// Principle 3
myBook.book();

// Principle 3
// code example for New Binding
function Book(chapters) {
this.phrase = chapters;
}

let myBook2 = new Book('Book Two');

console.log(myBook2.phrase);

// Principle 4
// code example for Explicit Binding
//.call
function friends(){
console.log(this.name);
}

let myFriend = {
name: 'James',
age: '25',
}

let myFriend2 = {
name: 'Jared',
age: '26',
}

friends.call(myFriend);
friends.call(myFriend2);

//.bind
function friends(){
console.log(this.name);
}

let myFriend1a = {
name: 'James',
age: '25',
}

let myFriend2a = {
name: 'Jared',
age: '26',
}

awesomeFriend1 = friends.bind(myFriend1a);
awesomeFriend2 = friends.bind(myFriend2a);

awesomeFriend1();
awesomeFriend2();

//.apply
var person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}

var person1 = {
firstName: "Mary",
lastName: "Doe"
}

// code example for Explicit Binding
person.fullName.apply(person1);