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
93 changes: 82 additions & 11 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,84 @@
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:
*/

/*
=== 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.`









/*
=== GameObject ===
*/
function GameObject(attributes) {
this.createdAt = attributes.createdAt,
this.name = attributes.name,
this.dimensions = attributes.dimensions
}

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

const halo = new GameObject({
createdAt: 34,
name: 'LC Carrier',
dimensions: '100 GB'
});
console.log(halo.destroy());


/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(attributes) {
GameObject.call(this, attributes),
this.healthPoints = attributes.healthPoints
}

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

const char = new CharacterStats({
createdAt: '19',
name: 'lc',
dimensions: '39',
healthPoints: 34
});
console.log(char.takeDamage());





function Humanoid(attributes) {
GameObject.call(this, attributes),
CharacterStats.call(this, attributes),

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

const huma = new Humanoid({
createdAt: '12-10-1992',
name: 'Superman',
dimension: '12 * 12',
team: 'Good Guys',
weapons: 'Snipper',
language: 'Chinese',
})

console.log(huma.takeDamage());


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -31,7 +93,7 @@
* 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
*/


/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand Down Expand Up @@ -104,6 +166,15 @@
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.
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
Expand Down
51 changes: 43 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
/* 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:
* 2. Implicit Binding:
* 3. New binding:
* 4. Explicit binding:
*
* write out a code example of each explanation above
*/

// Principle 1

// When in the global scope, the value of “this” will be the window/console Object;
// code example for Window Binding
function sayName(name) {
console.log(this);
return name;
}
sayName("Carrier");

// Principle 2

//Whenever a function is called by a preceding dot, the object before that dot is this.
// code example for Implicit Binding
const sayNameFunc = obj => {
obj.sayName = function() {
console.log(`Hello my name is ${this.name}`);
console.log(this);
};
};
const me = { name: 'LC' };
const you = { name: 'Carrier' };
sayNameFunc(me);
sayNameFunc(you);

// Invoke Methods on our objects
me.sayName();
you.sayName();

// Principle 3

//Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function.
// code example for New Binding
function CordialPerson(greeter) {
this.greeting = 'Hello ';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + this.greeter);
console.log(this);
};
}

const john = new CordialPerson('Carrier');
const carrier = new CordialPerson('John');

john.speak();
carrier.speak();

// Principle 4
//Whenever JavaScript’s call or apply method is used, this is explicitly defined.
// code example for Explicit Binding


// code example for Explicit Binding