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
37 changes: 34 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,53 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(gameAttributes) {
this.createdAt = gameAttributes.createdAt;
this.name = gameAttributes.name;
this.dimensions = gameAttributes.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(csAttributes) {
GameObject.call(this, csAttributes);
this.healthPoints = csAttributes.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
* team
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
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}.`;
}


/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +72,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 +133,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
49 changes: 44 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding: 'this' would be pointing to the window//console Object on the global scope.
* 2. Implicit Binding: 'this' points to the Object preceding the dot.
* 3. New Binding: 'this' points to the occurence of the object that is created by calling the Constructor function
using the 'new' keyword.
* 4. Explicit Binding: 'this' points to whatever is explicitly passed in
before the first comma in the call() or apply() function
*
* write out a code example of each explanation above
*/

// Principle 1

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

windObj();

// Principle 2

// code example for Implicit Binding
let mary = {
name: 'Mary',
greet: function(student) {
console.log(`Hello ${student} , my name is ${this.name}`);
}
}

mary.greet("Naromi");



// Principle 3

// code example for New Binding
function Model(height, weight, age) {
this.height = height;
this.weight = weight;
this.age = age;
}

let model1 = new Model('6 ft', ' 140 lbs', 22);

console.log(model1.height);

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
let lavendar = {
type: 'flower',
sayType: function() {
console.log(`I am a ${this.type}`);
}

};

let dandelion = {
type: "weed"
};

lavendar.sayType.call(dandelion);