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

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.

Each constructor function has unique properties and methods that are defined in their block comments below:
Each constructor function has unique properties and methods that are defined in their block comments below: Are you ready for some coding?
*/

/*
Expand All @@ -16,13 +16,39 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function gameObject(features) {

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.

it is a useful convention to always use CapitalCase when declaring Constructor functions. this has no effect on the way the code runs, but it provides an important visual cue that makes your code easier to read and understand to other developers and yourself.

function GameObject(features) {

this.createdAt = features.createdAt;
this.name = features.name;
this.dimensions = features.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.) ===
* team
Expand All @@ -32,6 +58,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(species) {
CharacterStats.call(this, species);
this.team = species.team;
this.weapons = species.weapons;
this.language = species.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 +80,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 @@ -92,6 +131,41 @@
language: 'Elvish',
});

const villian = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 12,
height: 10,
},
healthPoints: 110,
name: 'Sandman',
team: 'Kobra Kai',
weapons: [
'Mac-10',
'Machette',
],
language: 'Jive',
});

const hero = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 1,
},
healthPoints: 110,
name: 'Mini Me',
team: 'Hero',
weapons: [
'Halitosis',
'Super Strength',
],
language: 'Pig Latin',



console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -102,7 +176,8 @@
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.
*/
console.log(hero.)


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
65 changes: 46 additions & 19 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
/* 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---When in the global scope, the value of “this” will be the window/console Object
* 2. Implicit Binding---Whenever a function is called by a preceding dot, the object before that dot is this.
* 3. New binding---Whenever a constructor function is used
* 4. Explicit Binding---Whenever call or apply method is used, this is explicitly defined
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

// Principle 2

// code example for Implicit Binding

// Principle 3

// code example for New Binding

// Principle 4

// code example for Explicit Binding
// Principle 1---Window/Global Object Binding

function sayName(name) {
console.log(this);
return name;
}
sayName("Shire");


// Principle 2---Implicit Binding

const myObj = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
myObj.sayHello('Ian');


// Principle 3---New binding
class mackDaddy {
constructor(greeter) {
this.greeting = 'Hello ';
this.greeter = greeter;
this.speak = function () {
console.log(this.greeting + this.greeter);
console.log(this);
};
}
}

const eminem = new mackDaddy('Slim');
const iceCube = new mackDaddy('Shady');

eminem.speak();
iceCube.speak();

// Principle 4---Explicit Binding

eminem.speak.call(iceCube); iceCube.speak.apply(eminem);