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: 12 additions & 0 deletions assignments/debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[0330/125413.376:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/130413.378:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/130413.379:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/130913.378:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/132413.380:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/133913.382:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/135413.386:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/140913.387:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/142413.390:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/143913.391:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/145413.392:ERROR:crash_report_database_win.cc(428)] unexpected header
[0330/150913.396:ERROR:crash_report_database_win.cc(428)] unexpected header
33 changes: 31 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(obj) {
this.createdAt = obj.createdAt;
this.name = obj.name;
this.dimensions = obj.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;
}
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.) ===
Expand All @@ -32,6 +49,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attrs) {
CharacterStats.call(this, attrs);
this.team = attrs.team;
this.weapons = attrs.weapons;
this.language = attrs.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 +70,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 +131,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
62 changes: 57 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,78 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. If in the Global Scope, the value of 'this' is the Window/Console Object.
* 2. If a function is called with a . before it, the value of 'this'
is the Object on the left side of the period.
* 3. If a constructor function is used, 'this' refers to the specific Object instance
created and returned by constructor function
* 4. When .call(), .apply(), and .bind() are used, 'this' is explicitly defined
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this); // Window

// Principle 2

// code example for Implicit Binding
const anObj = {
name: "Douglas",
age: 35,
speak() {
console.log('implicit this === ', this);
return `Hi, I'm ${this.name} and I am ${this.age}.`;
}
}

anObj.speak(); /* implicit this === Object {
name: "Douglas",
age: 35,
speak() {
console.log('implicit this === ', this);
return `Hi, I'm ${this.name} and I am ${this.age}.`;
}
}
*/

// Principle 3

// code example for New Binding

function Car(attr) {
this.make = attr.make;
this.model = attr.model;
this.year = attr.year;
this.info = function() {
console.log('new binding ', this);
return `This is a ${this.year} ${this.make} ${this.model}.`;
}
}

const rangeRover = new Car({make: "Land Rover", model: "Range Rover", year: 2012});
const subaruWRX = new Car({make: "Subaru", model: "WRX", year: 2017});

rangeRover.info(); // new binding Car {make: "Land Rover", model: "Range Rover", year: 2012}
subaruWRX.info(); // new binding Car {make: "Subaru", model: "WRX", year: 2017}

// Principle 4

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

function ViceVersa(obj) {
this.name = obj.name;
this.age = obj.age;
this.speak = function() {
return `My name is ${this.name} now and I am ${this.age} now! What happened?`;
}
}

const marshall = new ViceVersa({name: "Marshall", age: 38});
const charlie = new ViceVersa({name: "Charlie", age: 11});

// Principle 4: Explicit Binding in effect below
console.log(marshall.speak.call(charlie)); // My name is Charlie now and I am 11 now! What happened?
console.log(charlie.speak.apply(marshall)); // My name is Marshall now and I am 38 now! What happened?