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: 42 additions & 5 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,60 @@

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

// FIRST ATTEMPT
// function GameObject(new Date, firstName, size, name) {
// this.createdAt = new Date();
// this.name = firstName;
// this.dimensions = size;
// this.destroy = function(name) {
// return `${this.name} 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.`;
};
/*
=== GameObject ===
* createdAt
* name
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
function CharacterStats(characterAttributes) {
GameObject.call(this, characterAttributes);
this.healthPoints = characterAttributes.healthPoints;
this.name = characterAttributes.name;
}

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

CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`;
};
/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function Humanoid(humanAttributes) {
CharacterStats.call(this, humanAttributes);
this.team = humanAttributes.team;
this.weapons = humanAttributes.weapons;
this.language = humanAttributes.language;
}

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

Humanoid.prototype.greet = function() {
return `${this.name} offers a greeting in ${this.language}`;
};
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,16 +70,15 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* 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 un-commenting these 3 objects and the list of console logs below:
// Test your 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 +139,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
50 changes: 45 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
/* 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
-called in the global scope
* 2. Implicit Binding
-called as a method (i.e. obj.func())
* 3. Explicit Binding
- called by call(), apply(), or bind()
* 4. New Binding
-called by the new keyword
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function car(){
console.log(this.sound);
}

const sound = "vroom";

sound();

// Principle 2

// code example for Implicit Binding

let myCar = {
name: "Maserati",
sound: "vroom",
car: function(){
console.log(this.sound);
}
};

myCar.car();

// Principle 3

// code example for New Binding

function Car(saying){
this.sound = saying;
}

let myCar = new Car("I am BumbleBee!");

console.log(myCar.sound);

// Principle 4

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

function car(){
console.log(this.sound);
}

let myCar = {
name: "Maserati",
sound: "vroom",
};

car.call(myCar);