Skip to content
Closed
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
156 changes: 152 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/

function GameObject(args){
this.createdAt = args.createdAt;
this.dimensions = args.dimensions;
}

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

/*
=== CharacterStats ===
* hp
Expand All @@ -23,6 +32,20 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(args){
this.hp = args.hp;
this.name = args.name;
GameObject.call(this, args);
}

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

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



/*
=== Humanoid ===
* faction
Expand All @@ -32,7 +55,25 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(args){
this.faction = args.faction;
this.weapons = args.weapons;
this.language = args.language;
CharacterStats.call(this, args);
}


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

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


console.log(CharacterStats.prototype);


/*
* Inheritance chain: Humanoid -> CharacterStats -> GameObject
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +82,7 @@

//Test you work by uncommenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -92,6 +133,7 @@
language: 'Elvish',
});


console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.hp); // 15
Expand All @@ -102,9 +144,115 @@
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 Villian and Hero classes that inherit from the Humanoid class.
// * Give the Hero and Villians 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;
// * Create two new objects, one a villian and one a hero and fight it out with methods!
// * Create two new objects, one a villian and one a hero and fight it out with methods!

function Villian(args){
Humanoid.call(this, args);
}

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

Villian.prototype.punch = function(){
let newhp = this.hp - 10;
if(newhp < 0){
this.destroy();
}
else{
return `your Hp is ${this.hp - 10}`;
}

}

Villian.prototype.kick = function(){
let newhp = this.hp - 20;
if(newhp < 0){
this.destroy();
}
else{
return `your Hp is ${this.hp - 20}`;
}
}


function Hero(args){
Humanoid.call(this, args);
}

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

Hero.prototype.punch = function(){
let newhp = this.hp - 10;
if(newhp < 0){
this.destroy();
}
else{
return `your Hp is ${this.hp - 10}`;
}

}

Hero.prototype.kick = function(){
let newhp = this.hp - 20;
if(newhp < 0){
this.destroy();
}
else{
return `your Hp is ${this.hp - 20}`;
}
}



const archer2 = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
hp: 100,
name: 'Lilith',
faction: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(archer2)

const swordsman2 = new Villian({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
hp: 90,
name: 'Sir Mustachio',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Toungue',
});

console.log(swordsman2)


console.log(archer2.punch());

console.log(archer2.kick());

console.log(swordsman2.punch());

console.log(swordsman2.kick());

55 changes: 49 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* The for principles of "this";
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. when 'this' is in global scope, it refrences the window or console object.
* 2. implicit binding: when object.function is called, 'this' (in the function) refers to the the object preceding the dot.
* 3. new binding: when the 'new' keyword is used to create an instance of an object, 'this' in the constructor funtion refers to that
object instance.
* 4. explicit binding: the 'call' and 'apply' methods explicitly state which object 'this' refers to.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +14,56 @@

// code example for Window Binding

function globalScope() {
console.log(this);
}

globalScope();

// Principle 2

// code example for Implicit Binding

const mySchool = {
school: 'Lambda',
gpa: function(gpa) {
console.log(`My gpa at ${this.school} is ${gpa}`);
}
};
mySchool.gpa('4.0');


// Principle 3

// code example for New Binding

function LambdaStudent(studentName, studentAge) {

this.name = studentName;
this.age = studentAge;
this.speak = function() {
console.log(`${this.name} is ${this.age} years old`);
};
}

const samarvir = new LambdaStudent('Samarvir', 21);
const sam = new LambdaStudent('Sam', 27);

samarvir.speak();
sam.speak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

console.log(samarvir);

let skills = ['HTML','CSS','JS'];

let sayName = function(skill1, skill2, skill3) {
console.log(this.name + ' can code in: ' + skill1 + ', ' + skill2 + ', ' + skill3 );
}

let newFunction = sayName.bind(samarvir, ...skills);

newFunction();