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
32 changes: 28 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@
* dimensions (These represent the character's size in the video game)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job here

* destroy() // prototype method -> returns the string: 'Object 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
* name
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats (Attributes) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job

GameObject.call(this, Attributes);
this.hp = Attributes.hp;
this.name = Attributes.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,7 +46,17 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (Attributes) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job

CharacterStats.call(this, Attributes);
this.faction = Attributes.faction;
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}.`;};
/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +65,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 +126,7 @@
console.log(archer.greet()); // Lilith offers a greeting in Elvish.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happen to the swordsman and archer???

console.log(mage.takeDamage()); // Bruce took damage.
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

several of these are not working because you're missing code.

*/


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
79 changes: 74 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good! short and concise answers that are correct and to the point. Love it.

*
* 1.
* 2.
* 3.
* 4.
* 1. Global: "this" points to the window object.
* 2. Implicit: "this" points to the object before the dot.
* 3. New: "this" is set within a constructor function and points to the created object.
* 4. Explicit: "this" points to instance that JavaScript’s call/apply method is used.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,83 @@

// code example for Window Binding

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job


let sayAge = function() {
console.log(this.name);
console.log(this.age);
};

let me = {
name: "Nathan",
age: 30
};

sayAge();

// Principle 2

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job, I like that you took the time to make multiple examples.

// code example for Implicit Binding

let Person = function(name, age) {
return {
name: name,
age: age,
sayName: function() {
console.log(`Hello, my name is ${this.name}.`);
},
josh: {
name: "Josh",
age: age,
sayName: function() {
console.log(`I'm ${this.name}.`);
}
},
steven: {
name: "Steven",
age: age,
sayName: function() {
console.log(`Nice to meet you two. My name is ${this.name}.`);
}
}
};
};

let nathan = Person("Nathan", 30);
nathan.sayName();
nathan.josh.sayName();
nathan.steven.sayName();

// Principle 3

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job


// code example for New Binding

let Person = function(name, age, race) {
this.name = name;
this.age = age;
this.race = race;
};

let mike = new Person("Mike", "40", "White");

console.log(mike);

// Principle 4

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job


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

let sayName = function(game1, game2, game3) {
console.log(`Hello. My name is ${this.name}. I love playing ${game1}, ${game2}, and ${game3}`);
};

let nathan = {
name: "Nathan",
age: 30
};

let games = ["MTGA", "EU4", "Ark"];

sayName.call(nathan, games[0], games[1], games[2])

sayName.apply(nathan, games)

let newFn = sayName.bind(nathan, games[0], games[1], games[2])
newFn();