Skip to content

Commit e3fc477

Browse files
authored
Merge pull request #1 from ogrotten/darrin-lowery
Darrin lowery
2 parents 33d1564 + 2668809 commit e3fc477

2 files changed

Lines changed: 168 additions & 79 deletions

File tree

assignments/prototypes.js

Lines changed: 122 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Each constructor function has unique properties and methods that are defined in their block comments below:
99
*/
10-
10+
1111
/*
1212
=== GameObject ===
1313
* createdAt
@@ -32,79 +32,129 @@
3232
* should inherit destroy() from GameObject through CharacterStats
3333
* should inherit takeDamage() from CharacterStats
3434
*/
35-
35+
3636
/*
37-
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
38-
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
39-
* Instances of CharacterStats should have all of the same properties as GameObject.
40-
*/
37+
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
38+
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
39+
* Instances of CharacterStats should have all of the same properties as GameObject.
40+
*/
4141

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

44-
/*
45-
const mage = new Humanoid({
46-
createdAt: new Date(),
47-
dimensions: {
48-
length: 2,
49-
width: 1,
50-
height: 1,
51-
},
52-
healthPoints: 5,
53-
name: 'Bruce',
54-
team: 'Mage Guild',
55-
weapons: [
56-
'Staff of Shamalama',
57-
],
58-
language: 'Common Tongue',
59-
});
60-
61-
const swordsman = new Humanoid({
62-
createdAt: new Date(),
63-
dimensions: {
64-
length: 2,
65-
width: 2,
66-
height: 2,
67-
},
68-
healthPoints: 15,
69-
name: 'Sir Mustachio',
70-
team: 'The Round Table',
71-
weapons: [
72-
'Giant Sword',
73-
'Shield',
74-
],
75-
language: 'Common Tongue',
76-
});
77-
78-
const archer = new Humanoid({
79-
createdAt: new Date(),
80-
dimensions: {
81-
length: 1,
82-
width: 2,
83-
height: 4,
84-
},
85-
healthPoints: 10,
86-
name: 'Lilith',
87-
team: 'Forest Kingdom',
88-
weapons: [
89-
'Bow',
90-
'Dagger',
91-
],
92-
language: 'Elvish',
93-
});
94-
95-
console.log(mage.createdAt); // Today's date
96-
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
97-
console.log(swordsman.healthPoints); // 15
98-
console.log(mage.name); // Bruce
99-
console.log(swordsman.team); // The Round Table
100-
console.log(mage.weapons); // Staff of Shamalama
101-
console.log(archer.language); // Elvish
102-
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103-
console.log(mage.takeDamage()); // Bruce took damage.
104-
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
44+
function GameObject(attr){
45+
this.createdAt = attr.createdAt;
46+
this.name = attr.name;
47+
this.dimensions = attr.dimensions;
48+
}
49+
GameObject.prototype.destroy = function() {
50+
return `${this.name} was removed from the game.`;
51+
}
52+
53+
function CharacterStats(attr) {
54+
this.healthPoints = attr.healthPoints;
55+
}
56+
CharacterStats.prototype = Object.create(GameObject.prototype);
57+
CharacterStats.prototype.takeDamage = function(){
58+
return `${this.name} took damage.`;
59+
}
60+
61+
function Humanoid(attr){
62+
this.team = attr.team;
63+
this.weapons = attr.weapons;
64+
this.language = attr.language;
65+
GameObject.call(this, attr);
66+
CharacterStats.call(this, attr);
67+
}
68+
Humanoid.prototype = Object.create(CharacterStats.prototype)
69+
70+
Humanoid.prototype.greet = function() {
71+
return `${this.name} offers a greeting in ${this.language}.`
72+
}
73+
74+
75+
const mage = new Humanoid({
76+
createdAt: new Date(),
77+
dimensions: {
78+
length: 2,
79+
width: 1,
80+
height: 1
81+
},
82+
healthPoints: 5,
83+
name: "Bruce",
84+
team: "Mage Guild",
85+
weapons: ["Staff of Shamalama"],
86+
language: "Common Tongue"
87+
});
88+
89+
const swordsman = new Humanoid({
90+
createdAt: new Date(),
91+
dimensions: {
92+
length: 2,
93+
width: 2,
94+
height: 2
95+
},
96+
healthPoints: 15,
97+
name: "Sir Mustachio",
98+
team: "The Round Table",
99+
weapons: ["Giant Sword", "Shield"],
100+
language: "Common Tongue"
101+
});
102+
103+
const archer = new Humanoid({
104+
createdAt: new Date(),
105+
dimensions: {
106+
length: 1,
107+
width: 2,
108+
height: 4
109+
},
110+
healthPoints: 10,
111+
name: "Lilith",
112+
team: "Forest Kingdom",
113+
weapons: ["Bow", "Dagger"],
114+
language: "Elvish"
115+
});
116+
117+
console.log(mage.createdAt); // Today's date
118+
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
119+
console.log(swordsman.healthPoints); // 15
120+
console.log(mage.name); // Bruce
121+
console.log(swordsman.team); // The Round Table
122+
console.log(mage.weapons); // Staff of Shamalama
123+
console.log(archer.language); // Elvish
124+
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
125+
console.log(mage.takeDamage()); // Bruce took damage.
126+
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
127+
128+
// Stretch task:
129+
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
130+
131+
function Villain(attr){
132+
this.lair = attr.lair;
133+
this.victimCount = attr.victimCount;
134+
this.adjective = attr.adjective;
135+
Humanoid.call(this, attr);
136+
}
137+
138+
Villain.prototype = Object.create(Humanoid.prototype);
139+
Villain.prototype.evile = function() {
140+
return `${this.name} the ${this.adjective} murders ${this.victimCount} babies in ${this.lair}.`
141+
}
142+
143+
const baddie = new Villain ({
144+
createdAt: new Date(),
145+
healthPoints: 10,
146+
name: "Milton",
147+
team: "Der",
148+
weapons: ["Axe of Hell"],
149+
language: "Stthmgol",
150+
151+
adjective: "Mad",
152+
victimCount: 1741,
153+
lair: "The Village of the Damned"
154+
})
155+
156+
console.log(baddie.evile()); // Bruce took damage.
157+
106158

107-
// Stretch task:
108-
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109-
// * 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;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
159+
// * 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;
160+
// * Create two new objects, one a villain and one a hero and fight it out with methods!

assignments/this.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. Global - without context, `this` is in the context of either the Global or Window object
5+
* 2. Implicit - when using an object method, `this` context is implied to be the object left of the dot
6+
* 3. New - when using a constructor, `this` context is of the object built by the constructor
7+
* 4. Explicit - when using bind/apply/call, `this` context explicitly defined.
8+
"half stretch" answer to all 4: `this` generally refers to the parent context of where it's used.
9+
In the case of Explicit, the value of `this` is overridden to be what you give it.
10+
(give example of something about Array())
811
*
912
* write out a code example of each explanation above
1013
*/
1114

1215
// Principle 1
1316

1417
// code example for Window Binding
18+
console.log(this.Array.prototype) // refers to array on the Window (in this case) object.
1519

1620
// Principle 2
17-
1821
// code example for Implicit Binding
22+
let kids = {
23+
first: "Schuyler",
24+
second: "Bennett",
25+
third: "Valkyrie",
26+
fourth: "Maxx",
27+
fifth: "DeLenn",
28+
middle: function(){
29+
console.log(`middle kid is ${this.third}`)
30+
}
31+
}
32+
kids.middle()
1933

2034
// Principle 3
21-
2235
// code example for New Binding
36+
function Kids (incoming) {
37+
this.incoming = incoming, // this `this` threw me for a minute
38+
this.first = "Schuyler",
39+
this.second = "Bennett",
40+
this.third = "Valkyrie",
41+
this.fourth = "Maxx",
42+
this.fifth = "DeLenn",
43+
this.favorite = function(){
44+
console.log(`My favorite kid is ${this.incoming}`);
45+
};
46+
}
47+
48+
const best = new Kids('all of em.');
49+
best.favorite();
50+
2351

2452
// Principle 4
53+
// code example for Explicit Binding
54+
55+
function car(){
56+
console.log(`${this.model}`);
57+
}
58+
59+
const carData = {
60+
make: "Honda",
61+
model: "Civic",
62+
year: "2005",
63+
}
2564

26-
// code example for Explicit Binding
65+
car.call(carData);

0 commit comments

Comments
 (0)