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
45 changes: 41 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
* 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 ('GameObject was removed from the game');
};



/*
=== CharacterStats ===
Expand All @@ -22,6 +32,18 @@
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(charAttributes){
GameObject.call(this, charAttributes);
this.hp = charAttributes.hp;
this.name = charAttributes.name;
}

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

CharacterStats.prototype.takeDamage = function(){
return ('GameObject took damage');
}


/*
=== Humanoid ===
Expand All @@ -31,17 +53,32 @@
* 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
*/
*/

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

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


Humanoid.prototype.greet = function() {
return (`Hello ${this.name} in his ${this.language} native tongue.`);
}


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

// 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 +139,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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
65 changes: 59 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/* 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. Window/Global Onject Binding
After writing an object,

* 2. Implcit Binding
when a function is invoked, whatever is to the left of the
dot, is what the keyword will be referencing
*
3. New Binding
making a main constructor and then using that to make new objects


* 4. Explicit Binding
.call, .apply
.call uses the .call method to reference 'this' then individually calls one array at a time starting with index 0
while apply does the same thing, but just needs a array name to reference all arrays
also bind is to lock something together in a new function to save and use for later
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +25,55 @@

// code example for Window Binding

const info = {
'name': this.name
};
console.log(info.name)


// Principle 2

// code example for Implicit Binding

const info = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't quite right on implicit binding. Implicit binding is when you call a method on an object, then any this in that method are bound to that object

'name': "Actaeon",
};

info.name

// Principle 3

// code example for New Binding

const Person = function(name, age, favorite_thing){
this.name;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite right on syntax.. this should be:

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

this.age;
this.favorite_thing
};

const the_dude = new Person('Dude', 40, 'white_russian');
const dudes_bestfriend = new Person('walter', 45, 'bowling')
console.log(the_dude);
console.log(dudes_bestfriend);

// Principle 4

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

const doSomething = function(thing1, thing2, thing3) {
console.log(this.name, this.age + ' ' + thing1 + ' ' + thing2 + ' ' + thing3)
}

const the_dude = {
'name': 'lebowski',
'age': 40
};

const thing = ['rug', 'bowling', 'white_russian'];
// call function
doSomething.call(the_dude, thing[0], thing[1], thing[2]);
// apply function
doSomething.apply(the_dude, thing);
// bind function
const savePoint = doSomething.bind(the_dude, thing[0], thing[1], thing[2]);
savePoint();