Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit c004823

Browse files
Gabe Rodriguezkvarak
Gabe Rodriguez
authored andcommitted
Adding week 3 lecture notes
1 parent d3c23d8 commit c004823

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Here you can find course content and homework for the JavaScript3 modules
99
|----|-----|----|--------|-------------|
1010
|1.|• [XMLHttpRequests](../../../fundamentals/blob/master/fundamentals/XMLHttpRequest.md) <br />• API calls <br />• Structure for a basic SPA (Single Page Application) <br />|[Reading Week 1](/Week1/README.md)|[Homework Week 1](/Week1/MAKEME.md)|[notes](/Week1/LECTURENOTES.md)
1111
|2.|• [Event Loop (order of execution)](../../../fundamentals/blob/master/fundamentals/event_loop.md)<br />• [Promises](../../../fundamentals/blob/master/fundamentals/promises.md)|[Reading Week 2](/Week2/README.md)|[Homework Week 2](/Week2/MAKEME.md)|[notes](/Week2/LECTURENOTES.md)
12-
|3.|[try...catch](../../../fundamentals/blob/master/fundamentals/try_catch.md)<br />• [async/await](../../../fundamentals/blob/master/fundamentals/async_await.md)<br />• [The `this` keyword](../../../fundamentals/blob/master/fundamentals/this.md)<br />• call, apply, bind<br />• [Object Oriented Programming and ES6 Classes](../../../fundamentals/blob/master/fundamentals/oop_classes.md)|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|
12+
|3.|• [try...catch](../../../fundamentals/blob/master/fundamentals/try_catch.md)<br />• [async/await](../../../fundamentals/blob/master/fundamentals/async_await.md)<br />• [The `this` keyword](../../../fundamentals/blob/master/fundamentals/this.md)<br />• call, apply, bind<br />• [Object Oriented Programming and ES6 Classes](../../../fundamentals/blob/master/fundamentals/oop_classes.md)|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|[notes](/Week3/LECTURENOTES.md)
1313

1414
__Kind note:__
1515

Week3/LECTURENOTES.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
## ES6 Classes
2+
- I'm trying to make a video game with characters that have names, health points, and armor points. What data structure could be used to represent a character?
3+
- An object nicely encapsulated
4+
```
5+
const character = {
6+
name: 'Sekiro',
7+
health: 100,
8+
armor: 20,
9+
}
10+
```
11+
- What if we wanted to give it attack methods?
12+
```
13+
const character = {
14+
name: 'Sekiro',
15+
health: 100,
16+
armor: 20,
17+
strength: 30,
18+
attack: (character) => {
19+
character.health -= strength;
20+
}
21+
}
22+
```
23+
- Each character, can use power-up once to increase their strength for 10 seconds. How do we implement that?
24+
```
25+
const character = {
26+
name: 'Sekiro',
27+
health: 100,
28+
armor: 20,
29+
strength: 30,
30+
attack: (opponent) => {
31+
opponent.health -= strength;
32+
},
33+
powerUpUsed: false
34+
}
35+
36+
const powerUp = character => {
37+
if (!character.powerUpUsed) {
38+
character.strength = character.strength * 2;
39+
character.powerUpUsed = true;
40+
}
41+
}
42+
43+
powerUp(character)
44+
```
45+
- but what if we want to do... `character.powerUp()`?
46+
47+
```
48+
const character = {
49+
name: 'Sekiro',
50+
health: 100,
51+
armor: 20,
52+
strength: 30,
53+
attack: (opponent) => {
54+
opponent.health -= strength;
55+
},
56+
_powerUpReady = true,
57+
powerUp: () => {
58+
if (_powerUpReady) {
59+
this.strength = this.strength * 2;
60+
setTimeout(() => {
61+
this.strength = this.strength / 2;
62+
}, 10000)
63+
_powerUpReady = false;
64+
}
65+
}
66+
}
67+
```
68+
- Why does _powerUpReady have an underscore? What's different about it?
69+
- Great! But... we need to start create many of these---one for each character. But if we just copy/paste, we will be copying a lot of code redudantly. This is most obvious when it comes to the attack and powerUp methods. What shall we do!?
70+
71+
- In the old days ---> constructor functions
72+
- Uppercase constructor
73+
```
74+
function Character(name, strength) {
75+
this.name = name;
76+
this.health = 100;
77+
this.armor = 20;
78+
this.strength = strength;
79+
this._powerUpReady = true,
80+
}
81+
82+
Character.prototype.attack = (character) => {
83+
character.health -= strength;
84+
}
85+
Character.prototype.powerUp: () => {
86+
if (this._powerUpReady) {
87+
this.strength = this.strength * 2;
88+
setTimeout(() => {
89+
this.strength = this.strength / 2;
90+
}, 10000)
91+
this._powerUpReady = false;
92+
}
93+
}
94+
```
95+
- Then you could instantiate it via...
96+
```
97+
const flash = new Character('Flash Gordon', 10)
98+
```
99+
- Enter ES6 classes
100+
```
101+
class Character {
102+
health = 100;
103+
armor = 20;
104+
_powerUpReady = true;
105+
106+
constructor(name, strength) {
107+
this.name = name;
108+
this.strength = strength;
109+
}
110+
111+
attack(character) {
112+
character.health -= this.strength;
113+
}
114+
115+
powerUp() {
116+
if (this._powerUpReady) {
117+
this.strength = this.strength * 2;
118+
setTimeout(() => {
119+
this.strength = this.strength / 2;
120+
}, 10000)
121+
this._powerUpReady = false;
122+
}
123+
}
124+
}
125+
```
126+
127+
- Prototypes
128+
- Array methods, where do they come from?
129+
- Everything is an object
130+
- instanceOf
131+
- Object.isPrototypeOf() & .\_\_proto\_\_
132+
133+
- Inheritance
134+
- We want to create bosses. They have everything a normal character has, but with more health, an evil tagline, and a self-heal method.
135+
- We can use extend!
136+
```
137+
class Boss extends Character {
138+
health = 200;
139+
140+
constructor(name, strength, tagline) {
141+
super(name, strength);
142+
this.tagline = tagline;
143+
}
144+
145+
sayTagline() {
146+
console.log(this.tagline)
147+
}
148+
149+
selfHeal() {
150+
this.health += 20;
151+
}
152+
}
153+
```

0 commit comments

Comments
 (0)