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

function GameObject(attrs) {
this.createdAt = attrs.createdAt;
this.name = attrs.name;
this.dimensions = attrs.dimensions;
this.destroy = function(){
console.log(`${this.name} was removed from the game`);
};
}

// 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){
this.healthPoints = stats.healthPoints;
GameObject.call(this,stats);
this.areStats = stats.areStats;
this.takeDamage = function(){
return `${this.name} took damage`;
};
};

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

CharacterStats.prototype = Object.create(GameObject.prototype);
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,16 +58,49 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(human){
this.team = human.team;
this.weapons = human.weapons;
this.language = human.language;
GameObject.call(this,human);
CharacterStats.call(this,human);
this.greet = function(){
return `${this.name} offers a greeting in ${this.language}`;
}}
// Humanoid.prototype.greet = function(){
// return `${this.name} offers a greeting in ${this.language}`;
// }

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

/*
* 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 Villain(villain){
Humanoid.call(this,villain);
this.attack = function(character){
character.healthPoints -=5;
// console.log(character.healthPoints);
if(character.healthPoints == 0){
character.destroy();
}
}
}

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

function Hero(hero){
Villain.call(this,hero);
}

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

// 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 +151,39 @@
language: 'Elvish',
});

const newVillain = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 20,
name: 'evil',
team: 'evil',
weapons: [
'evil stuff',
],
language: 'Common Tongue',
});


const newHero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 20,
name: 'good',
team: 'good',
weapons: [
'gooder stuff',
],
language: 'Common Tongue',
});

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 +194,17 @@
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(newVillain.attack(mage));
newVillain.attack(newHero);
newHero.attack(newVillain);
newVillain.attack(newHero);
newHero.attack(newVillain);
newVillain.attack(newHero);
newHero.attack(newVillain);
newVillain.attack(newHero);
newHero.attack(newVillain);



// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
47 changes: 38 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
*/


// Window-Global Object Binding: this refers to the object of the browser window you are using, so it is the whole of the browser window in which you are working.
// Implicit Binding: when a function is called/invoked, the object preceding the .(dot) is what .this is referring to.
// New Binding: in the use of a constructor function, the .this refers to the specific instance of the newly created object by the constructor.
// Explicit Binding: the use of all or apply on an object, the .this is explicitly defined when either method is called.


// Principle 1

// code example for Window Binding

console.log(this);
// Principle 2

// code example for Implicit Binding
// function sayMyName(bandName){
// console.log(this);
// return bandName;
// }
// sayMyName("Destiny's Child");

const sayMyName = obj => {
obj.sayMyName = function() {
console.log(`Say my name, Say my name by ${this.name}`);
};
};
const dc = { name: "Destiny's Child" };

sayMyName(dc);
// Invoke Methods on our objects
dc.sayMyName();


// Principle 3

// code example for New Binding

function CatHerder(herder) {
this.name = herder;
this.herd = function() {
console.log(`My name is ${this.name} and I'm the best cat herder in the world.`);
}
}

const gunner = new CatHerder('Gunner');

gunner.herd();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
gunner.herd.call(dc);