Skip to content
Closed
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
38 changes: 36 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
* dimensions
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/
function GameObject(gObjAttribute) {
this.createdAt = gObjAttribute.createdAt;
this.dimensions = gObjAttribute.dimensions;
}

GameObject.prototype.destroy = function() {
return `${this.name} was removed from the game.`;
};

/*
=== CharacterStats ===
Expand All @@ -22,6 +30,17 @@
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(crtsAttribute) {
GameObject.call(this, crtsAttribute);
this.hp = crtsAttribute.hp;
this.name = crtsAttribute.name;
}

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

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

/*
=== Humanoid ===
Expand All @@ -38,10 +57,25 @@
* 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.
*/
function Humanoid(hmAttribute) {
CharacterStats.call(this, hmAttribute);
this.faction = hmAttribute.faction;
this.weapons = hmAttribute.weapons;
this.language = hmAttribute.language;
}

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

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

//Stretch Goal


//Test you work by uncommenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,7 +136,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 Villian and Hero classes that inherit from the Humanoid class.
Expand Down
51 changes: 42 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,59 @@
/* 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 - Global context. Without Implicit, explicit and new to tell what this should point to, this will goint to dafualt to the window object.
* 2. Implicit Binding - Look to the left on the dot. Whatever it is, it bocomes the context of this function.
* 3. New Binding - when we invoke a function (that has this keyword) with new keyword, behind the scene JS will ceate a new object and save it like this = {}.
* 4. Explicit Binding - It tells the function what object should use for this, using functions such as call, apply and bind.
*
* write out a code example of each explanation above
*/

// Principle 1

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

// Principle 2

// code example for Implicit Binding
let car = {
name: 'Honda',
model: 'Accord',
year: 1998,
toDealer: function() {
console.log(`30% discount for ${this.year} ${this.name} ${this.model}.`);
}
}
car.toDealer();

// Principle 3

// code example for New Binding

// Principle 4
let Car = function(name, model, year) {
this.name = name;
this.model = model;
this.year = year;
}

let newCar = new Car('Toyota', 'Camry', 2017);
console.log(newCar);

// code example for Explicit Binding
// Principle 4
// code example for Explicit Binding
let student1 = {
name: 'Ja',
major: 'CS'
};

let whatILike = function(sub1, sub2, sub3) {
console.log(`I am ${this.name} and I like ${sub1}, ${sub2}, and ${sub3}`);
}

let subjects = ['Math', 'Programming Languages', 'Statistics'];

whatILike.call(student1, subjects[0], subjects[1], subjects[2]);
whatILike.apply(student1, subjects);
let forStudent2 = whatILike.bind(student1, subjects); //.bind - create new function named forStudent2 in case we use it later.
forStudent2();