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
44 changes: 37 additions & 7 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,63 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
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.`;
}

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

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
}*/

function CharacterStats(characterStatsattributes){
console.log(characterStatsattributes);

GameObject.call(this , characterStatsattributes);
this.healthPoints = characterStatsattributes.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
* 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
*/


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

function Humanoid(humanoidattributes){
console.log(humanoidattributes);
GameObject.call(this, humanoidattributes);
Humanoid.prototype = Object.create(CharacterStats.prototype);
this.team = humanoidattributes.team;
this.weapons = humanoidattributes.weapons;
this.language = humanoidattributes.language;
Humanoid.prototype.greet =function(){
return `${this.name} offers a greeting in ${this.language}`;
}

}
// 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 +132,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
38 changes: 33 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. this keyword:New binding when you make a new object
* 2. implicit binding :Whenever a function is called by a preceding dot, the object before that dot is this.
* 3. window binding window crystal ball
* 4. Whenever JavaScript’s 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
function sayName(name) {
console.log(this);
return name;
}
sayName("Nerissa");

// Principle 2

// code example for Implicit Binding
const myObj = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
myObj.sayHello('Sean');

// Principle 3

// 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 jerry = new CordialPerson('Newman');
const newman = new CordialPerson('Jerry');

jerry.speak();
newman.speak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
jerry.speak.call(newman); newman.speak.apply(jerry);