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
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</head>

<body>
<h1>JS III - Check your work in the console!</h1>
<h1>JS III - Check your work in the console!</h1>
</body>
</html>
167 changes: 101 additions & 66 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
* dimensions (These represent the character's size in the video game)
* 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.`;
}

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

you need to call the parent here

this.name = attributes.name;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

object.create parent here

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.) ===
Expand All @@ -32,6 +54,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(attributes) {
CharacterStats.call(this, attributes);
this.team = attributes.team;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

parent call here

this.weapons = attributes.weapons;
this.language = attributes.language;

}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

object.create parent here

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

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

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,70 +76,70 @@

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

/*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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.
// * Give the Hero and Villains 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 villain and one a hero and fight it out with methods!
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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.
// * Give the Hero and Villains 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 villain and one a hero and fight it out with methods!
66 changes: 58 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,76 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. window - everything there is, no specificity
* 2. implicit - binding that happens automatically
* 3. new - constructor function thats used to easily duplicate objects
* 4. explicit - binding that that is forced/controlled
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function hello() {
console.log("Principle 1:", this)
}

// Principle 2
hello();

// Principle 2
// code example for Implicit Binding
const person = {
name: "Brandi",
age: 36,
fun: function() {
console.log("Principle 2:", `${this.name} is ${this.age} years old`);
}
}

// Principle 3
person.fun();

// Principle 3
// code example for New Binding
function TeenTitan(member) {
this.member = member;
this.greeting = "Hey!";
this.speak = function() {
console.log("Principle 3a:", `${this.greeting}, ${this.member}`)
}
}

// const robin = {
// name: "Robin"
// }

const cyborg = new TeenTitan("Cyborg");

console.log("Principle 3b:", cyborg)
cyborg.speak();

// Principle 4
// code example for Explicit Binding
const child = {
name: "Sonnet"
}

const skills = ['cooking', ' programming', ' comedy'];
const pets = ['ferret', 'pigeon', 'rabbit'];

//using call
function facts1(skills) {
console.log("Principle 4a call:", `Hello, my name is ${this.name}. I like: ${skills}.`);
};

facts1.call(child, skills);


//using apply
//using call with spread operator
function facts2(pet1, pet2, pet3) {

console.log("Principle 4b apply and call with spread:", `Hello, my name is ${this.name}. I have a: ${pet1}, ${pet2}, and ${pet3}.`);
};

// code example for Explicit Binding
facts2.apply(child, pets);
facts2.call(child, ...pets);