Skip to content

Commit a16d791

Browse files
committed
this.js complete. prototype.js 60% complete.
1 parent 1eb55ed commit a16d791

2 files changed

Lines changed: 91 additions & 24 deletions

File tree

‎assignments/prototypes.js‎

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
1616
*/
1717

18+
GameObject.prototype = Object.create(Humanoid.prototype);
19+
function GameObject(gameobjectbutes){
20+
this.createdAt = gameobjectbutes.createdAt;
21+
this.dimensions = gameobjectbutes.dimensions;
22+
//CharacterStats.call(this, gameobjectbutes);
23+
Humanoid.call(this, gameobjectbutes);
24+
}
25+
26+
GameObject.prototype.destroy = function(){
27+
return `Object was removed from the game.`
28+
}
29+
30+
1831
/*
1932
=== CharacterStats ===
2033
* healthPoints
@@ -23,6 +36,19 @@
2336
* should inherit destroy() from GameObject's prototype
2437
*/
2538

39+
CharacterStats.prototype = Object.create(Humanoid.prototype);
40+
function CharacterStats(characterbutes){
41+
//Humanoid.call(this, characterbutes);
42+
GameObject.call(this, characterbutes);
43+
this.healthPoints = characterbutes.healthPoints;
44+
this.name = characterbutes.name;
45+
}
46+
47+
48+
CharacterStats.prototype.takeDamage = function(){
49+
return `${this.name} took damage.`;
50+
}
51+
2652
/*
2753
=== Humanoid (Having an appearance or character resembling that of a human.) ===
2854
* team
@@ -32,7 +58,17 @@
3258
* should inherit destroy() from GameObject through CharacterStats
3359
* should inherit takeDamage() from CharacterStats
3460
*/
35-
61+
function Humanoid(attributes) {
62+
//CharacterStats.call(this, attributes);
63+
//GameObject.call(this, attributes);
64+
this.team = attributes.team;
65+
this.weapons = attributes.weapons;
66+
this.language = attributes.language;
67+
}
68+
Humanoid.prototype.great = function(){
69+
return `${this.team} says hello in ${this.language}`;
70+
}
71+
3672
/*
3773
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
3874
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
@@ -41,9 +77,9 @@
4177

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

44-
/*
80+
4581
const mage = new Humanoid({
46-
createdAt: new Date(),
82+
createdAt: Date.now(),
4783
dimensions: {
4884
length: 2,
4985
width: 1,
@@ -58,8 +94,10 @@
5894
language: 'Common Tongue',
5995
});
6096

97+
console.log(mage);
98+
6199
const swordsman = new Humanoid({
62-
createdAt: new Date(),
100+
createdAt: Date.now(),
63101
dimensions: {
64102
length: 2,
65103
width: 2,
@@ -76,7 +114,7 @@
76114
});
77115

78116
const archer = new Humanoid({
79-
createdAt: new Date(),
117+
createdAt: Date.now(),
80118
dimensions: {
81119
length: 1,
82120
width: 2,
@@ -91,18 +129,19 @@
91129
],
92130
language: 'Elvish',
93131
});
132+
133+
134+
// console.log(mage.createdAt); // Today's date
135+
// console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
136+
// console.log(swordsman.healthPoints); // 15
137+
// console.log(mage.name); // Bruce
138+
// console.log(swordsman.team); // The Round Table
139+
// console.log(mage.weapons); // Staff of Shamalama
140+
// console.log(archer.language); // Elvish
141+
// console.log(archer.great()); // Lilith offers a greeting in Elvish.
142+
// console.log(mage.takeDamage()); // Bruce took damage.
143+
// console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
94144

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

107146
// Stretch task:
108147
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.

‎assignments/this.js‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
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. Window/Global Object binding - If not contained within an Object scope, 'this' has global scope.
5+
6+
* 2. Implicit Binding - methods/functions called with dot notation will automatically inherit parent scope with 'this' prefixed.
7+
8+
* 3. New bindning - wwhen used with a constructor function 'this' refers to the object it is being called on.
9+
10+
* 4. Explicit binding - refers to the use of call and apply, 'this' is included as the first argument and denotes the Object the calling method invokes.
811
*
912
* write out a code example of each explanation above
1013
*/
1114

1215
// Principle 1
1316

14-
// code example for Window Binding
17+
//console.log(this);
1518

1619
// Principle 2
1720

18-
// code example for Implicit Binding
21+
// const speak = {
22+
// phrase : 'Hi',
23+
// deny : function(){
24+
// return this.phrase + ', ' + 'dont wanna talk rn.';
25+
// }
26+
// }
27+
28+
//console.log(speak.deny());
1929

2030
// Principle 3
31+
function makeCereal(kind){
32+
this.Kind = kind;
33+
this.eat = function(){
34+
console.log(`Im in a ${this.Kind} kind of mood. Think I'll have some captain crunch.`);
35+
}
36+
}
37+
const honeyNut = new makeCereal('healthy');
38+
const captain = new makeCereal('fun');
39+
40+
captain.eat();
2141

22-
// code example for New Binding
2342

2443
// Principle 4
2544

26-
// code example for Explicit Binding
45+
function Test(arg1, arg2){
46+
arg1 = 0;
47+
arg2 = 1;
48+
return (arg2 + arg2) / (arg1 + (arg2 + 4));
49+
}
50+
function Test2(){
51+
return Test.call(this, 2, 3);
52+
}
53+
54+
console.log(Test2());

0 commit comments

Comments
 (0)