|
| 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