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
1 change: 1 addition & 0 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<!--<script src="this.js"></script>-->
<script src="prototypes.js"></script>
<script src="this.js"></script>
</head>

<body>
Expand Down
62 changes: 54 additions & 8 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

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

/*
Expand All @@ -32,12 +32,13 @@ GameObject.prototype.destroy = function() {
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(charStats) {
this.healthPoints = charStats.healthPoints;
GameObject.call(this, charStats);
this.healthPoints = charStats.healthPoints;

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

/*
Expand Down Expand Up @@ -133,5 +134,50 @@ 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.

const villian = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'BadGuy',
team: 'Team Evil',
weapons: [
'Bow',
'Dagger',
],
language: 'Common',

});

const hero = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'GoodFellas',
team: 'Team Goodies',
weapons: [
'Spear',
'Shield',
],
language: 'Common',
});

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

// * Create two new objects, one a villain and one a hero and fight it out with methods!

//methods
Humanoid.prototype.bowAttack = function(victim) {
victim.healthPoints = victim.healthPoints - 4;
},
villian.bowAttack(hero);
//This method should lower the hero's healthPoint stat by 4 per attack.
console.log(hero.healthPoints);
30 changes: 20 additions & 10 deletions assignments/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@ console.log(this);
// Principle 2
// code example for Implicit Binding
const tome = {
title: "Awesome Book!",
pages: 100,
cover: "hard",
}
console.log(tome.pages) //in this case TOME object is at the left of the dot
console.log(this); // THIS is accessing the CONSOLE because console is the object to the left of the DOT.
sayAuthor: function(name) {
console.log(`${this.title} my author is ${name}`);
console.log(this);
}
};
tome.sayAuthor("Warren"); //calling the function the THIS refers to the object which is to the left of the DOT. In this case TOME.

console.log(this); // THIS is accessing the CONSOLE because console is the object to the left of the DOT. This makes sense for this one because it's outside of the object. The other console.log inside the object is going to referr to the object TOME.

// Principle 3

// code example for New Binding
function Book(attr) {
this.title = attr.title;
this.author = attr.author;
this.subject = attr.subject;
function Book(title, author, subject) {
this.title = title;
this.author = author;
this.subject = subject;
this.present = function() {
console.log('our new book is ${this.title} by ${this.author}.');
console.log(`our new book is ${this.title} by ${this.author}.`);
}
}
const hobbit = new Book { title: "the Hobbit", author: "Tolkein", subject: "Fantasy" };
const hobbit = new Book("the Hobbit", "Tolkein", "Fantasy");
hobbit.present();

// Principle 4

//code example for Explicit Binding
Expand All @@ -50,4 +58,6 @@ function Comic(name, price) {
}

console.log(new Comic('x-factor', 5).name);
console.log(new Comic('x-men', .25).price);
console.log(new Comic('x-men', .25).price);
const xMen = new Comic("x-men", 2.50)
console.log(xMen); // construct the X-Men Comic (BookSet) with a category of Comic built into the constructor.