Skip to content

Commit fe4b8e2

Browse files
Working on some pair programming with Jenny and Steven. Got the first part of the prototypes.js homework working.
1 parent 33d1564 commit fe4b8e2

2 files changed

Lines changed: 105 additions & 10 deletions

File tree

assignments/prototypes.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,50 @@
1616
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
1717
*/
1818

19+
function GameObject(attributes) {
20+
this.createdAt = Date(),
21+
this.name = attributes.name;
22+
this.dimensions = attributes.dimensions;
23+
}
24+
25+
GameObject.prototype.destroy = function destroy(){
26+
return `${this.name} was removed from the game.`;
27+
};
28+
29+
30+
//This below example works. Prototype inheritance is working, destroy is linked to the GameObject prototype.
31+
// const box = new GameObject({
32+
// createdAt: Date(),
33+
// name: "boxy",
34+
// dimensions: {
35+
// length: 2,
36+
// width: 2,
37+
// height: 2,
38+
// },
39+
// });
40+
41+
// console.log(box);
42+
43+
1944
/*
2045
=== CharacterStats ===
2146
* healthPoints
2247
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
2348
* should inherit destroy() from GameObject's prototype
2449
*/
2550

51+
52+
function CharacterStats(attributes) {
53+
GameObject.call(this, attributes)
54+
this.healthPoints = attributes.healthPoints;
55+
}
56+
57+
CharacterStats.prototype.takeDamage = function takeDamage() {
58+
return `${GameObject.name} took damage.`;
59+
}
60+
61+
62+
2663
/*
2764
=== Humanoid (Having an appearance or character resembling that of a human.) ===
2865
* team
@@ -39,7 +76,7 @@
3976
* Instances of CharacterStats should have all of the same properties as GameObject.
4077
*/
4178

42-
// Test you work by un-commenting these 3 objects and the list of console logs below:
79+
// Test your work by un-commenting these 3 objects and the list of console logs below:
4380

4481
/*
4582
const mage = new Humanoid({

assignments/this.js

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,84 @@
1-
/* The for principles of "this";
2-
* in your own words. explain the four principle for the "this" keyword below.
1+
/* The four principles of "this";
2+
* in your own words. explain the four principles for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. When a function is inside the global scope, 'this' refers to the window/console object.
5+
* 2. When used with dot notation, the object before the dot is what 'this' is referring to.
6+
* 3. Whenever we're using a constructor function, 'this' refers to the specific instance of that object that's created and returned by the constructor function.
7+
* 4. Whenever we're using the call or apply methods, the object that 'this' is referring to is explicitly defined. This is as specific as we go.
88
*
99
* write out a code example of each explanation above
1010
*/
1111

1212
// Principle 1
1313

1414
// code example for Window Binding
15+
// We're using a function inside the global scope, so the 'this' refers to the entire window/console here.
1516

16-
// Principle 2
17+
// function sayName(name) {
18+
// console.log(this);
19+
// return name;
20+
// }
21+
// sayName('Stephen');
1722

18-
// code example for Implicit Binding
23+
// // Principle 2
24+
25+
// // code example for Implicit Binding
26+
// // It is implied that since we're using dot notation, whatever is to the left of the dot is the object we're referring to.
27+
28+
// const myObj = {
29+
// greeting: 'Hello',
30+
// sayHello: function(name) {
31+
// console.log(`${this.greeting} my name is ${name}`);
32+
// console.log(this);
33+
// }
34+
// };
35+
// myObj.sayHello('Stephen');
1936

2037
// Principle 3
2138

2239
// code example for New Binding
2340

41+
// function CordialPerson(greeter) {
42+
// this.greeting = 'Hello ';
43+
// this.greeter = greeter;
44+
// this.speak = function() {
45+
// console.log(this.greeting + this.greeter);
46+
// // console.log(this);
47+
// };
48+
// }
49+
50+
// const jerry = new CordialPerson('Newman');
51+
// const newman = new CordialPerson('Jerry');
52+
53+
// jerry.speak();
54+
// //when Jerry speaks, he references the argument that was provided in CordialPerson('Newman').
55+
// //This is passed along via the 'this' bindings - this.greeting = 'Hello ' plus this.greeter = greeter (which we already provided a value for via the argument.)
56+
57+
// newman.speak();
58+
59+
// //Same thing here when Newman speaks, except in this case Jerry has been provided as the argument instead of Newman's name.
60+
2461
// Principle 4
2562

26-
// code example for Explicit Binding
63+
// code example for Explicit Binding
64+
65+
function CordialPerson(greeter) {
66+
this.greeting = 'Hello ';
67+
this.greeter = greeter;
68+
this.speak = function() {
69+
console.log(this.greeting + this.greeter);
70+
console.log(this);
71+
};
72+
}
73+
74+
const jerry = new CordialPerson('Newman');
75+
const newman = new CordialPerson('Jerry');
76+
77+
jerry.speak.call(newman);
78+
//with jerry.speak.call, we've re-bound jerry to reference newman instead. So now that we've re-bound jerry to newman, the jerry CordialPerson is going to behave like newman.
79+
80+
newman.speak.apply(jerry);
81+
//likewise with newman.speak.apply, we've re-bound newman to reference jerry instead. It will behave like jerry instead of newman and will take the arguments that the original jerry object used. The difference here is only that .apply will expect a second argument to be an array that it uses as arguments for the function.
82+
83+
jerry.speak();
84+
newman.speak();

0 commit comments

Comments
 (0)