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
58 changes: 54 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,57 @@
you will be implementing several classes with their correct inheritance heirarchy.

In this file you will be creating three classes: GameObject, CharacterStats, Humanoid.
At the bottom of this file are 3 objects that all inherit from Humanoid. Use the objects at the bottom of the page to test your classes.
At the bottom of this file are 3 objects that all inherit from Humanoid.
Use the objects at the bottom of the page to test your classes.

Each class has unique properites and methods that are defined in their block comments below:
*/

/*
/*
=== GameObject ===
* createdAt
* dimensions
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/
function GameObject(attributes){
this.createdAt = attributes.createdAt;
this.dimensions = attributes.dimensions;
}

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

console.log(GameObject.prototype.destroy());



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


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

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

// console.log(CharacterStats.prototype.destroy());


console.log(CharacterStats.prototype.destroy());
console.log(CharacterStats.prototype.takeDamage());

/*
=== Humanoid ===
Expand All @@ -39,9 +71,28 @@
* Instances of CharacterStats should have all of the same properties as GameObject.
*/

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

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

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

}

console.log(Humanoid.prototype.greet());



//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 @@ -102,7 +153,6 @@
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.
Expand Down
105 changes: 95 additions & 10 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,111 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*

When trying to figure out what the this keyword is,
you need to figure where the function was invoked.
----------------------------------------------------------------------------------------
* 1. Window Binding --
If you invoke a function that uses the this keyword but doesn't have the
features of the three following principles (implicit, explicit, new), the
this keyword defaults to the window object.
----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------
* 2. Implicit Binding --
Left of the dot at the time of the function call.
----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------
* 3. New Binding --
When a function is invoked with the new keyword, the this is bound
to the new object that is being constructed with 'new'.
----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------
* 4. Explicit Binding --
Explicit binds are called explicitly (obvi) using call, apply and bind.
----------------------------------------------------------------------------------------

* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let sayAge = function(){
return this.age;
}

// Principle 2
let myAge = 24;

console.log(sayAge());


// Principle 2
// code example for Implicit Binding

// Principle 3
const sayMyName = function(obj){

obj.sayName = function(){
return this.name;
};

}

const me = {
name: 'Alex',
age: 24
};


const you = {
name: 'John',
age: 27
};

sayMyName(me);
sayMyName(you);

console.log(me.sayName());
console.log(you.sayName());



// Principle 3
// code example for New Binding

let Dinosaur = function(name, age){

this.name = name;
this.age = age;

}

let tRex = new Dinosaur('tyrannosaurus', '65mill');
let raptor = new Dinosaur('velociraptor', '66mill');
let stego = new Dinosaur('stegosaurus', '70mill');

console.log(tRex);
console.log(raptor);
console.log(stego);




// Principle 4
// code example for Explicit Binding


let sayName = function(language1, language2, language3){
return 'Hello my name is ' + this.name + ' and I like to program using' + language1 + language2 + language3;
};

let alejandro = {
name: 'Alejandro',
age: 24
};

let languages = ['javascript', 'CSS', 'HTML'];



// code example for Explicit Binding
console.log(sayName.apply(alejandro, languages));