|
1 | 1 | # Object-Oriented Programming & Classes
|
2 | 2 |
|
3 |
| -### Object literals and Imperative Programming |
| 3 | +### Object Literals and Imperative Programming |
4 | 4 |
|
5 | 5 | In earlier JavaScript lectures we saw that we can define object literals and use functions to access and manipulate their properties. A typical example is shown below, where we have defined an array of objects, in this case each representing the name of a month and its associated number of days.
|
6 | 6 |
|
@@ -63,6 +63,70 @@ months
|
63 | 63 | .forEach(string => console.log(string));
|
64 | 64 | ```
|
65 | 65 |
|
| 66 | +### Constructor Functions (pre-ES6) |
| 67 | + |
| 68 | +```js |
| 69 | +function Month(name, days) { |
| 70 | + this.name = name; |
| 71 | + this.days = days; |
| 72 | +} |
| 73 | + |
| 74 | +const months = [ |
| 75 | + new Month('January', 31), |
| 76 | + new Month('February', 28), |
| 77 | + new Month('March', 31), |
| 78 | + new Month('April', 30), |
| 79 | + new Month('May', 31), |
| 80 | + new Month('June', 30), |
| 81 | + new Month('July', 31), |
| 82 | + new Month('August', 31), |
| 83 | + new Month('September', 30), |
| 84 | + new Month('October', 31), |
| 85 | + new Month('November', 30), |
| 86 | + new Month('December', 31) |
| 87 | +]; |
| 88 | + |
| 89 | +months |
| 90 | + .filter(month => month.days === 31) |
| 91 | + .map(month => `${month.name} has ${month.days} days.`) |
| 92 | + .forEach(string => console.log(string)); |
| 93 | +``` |
| 94 | + |
| 95 | +### JavaScript Functions and `this` |
| 96 | + |
| 97 | +[Understanding JavaScript Function Invocation and "this"](http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/), by Yehuda Katz. |
| 98 | + |
| 99 | +### ES6 Classes vs pre-ES6 Constructor Functions |
| 100 | + |
| 101 | +```js |
| 102 | +class Month { |
| 103 | + constructor(name, days) { |
| 104 | + this.name = name; |
| 105 | + this.days = days; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const months = [ |
| 110 | + new Month('January', 31), |
| 111 | + new Month('February', 28), |
| 112 | + new Month('March', 31), |
| 113 | + new Month('April', 30), |
| 114 | + new Month('May', 31), |
| 115 | + new Month('June', 30), |
| 116 | + new Month('July', 31), |
| 117 | + new Month('August', 31), |
| 118 | + new Month('September', 30), |
| 119 | + new Month('October', 31), |
| 120 | + new Month('November', 30), |
| 121 | + new Month('December', 31) |
| 122 | +]; |
| 123 | + |
| 124 | +months |
| 125 | + .filter(month => month.days === 31) |
| 126 | + .map(month => `${month.name} has ${month.days} days.`) |
| 127 | + .forEach(string => console.log(string)); |
| 128 | +``` |
| 129 | + |
66 | 130 | ### ES6 Classes and Object-Oriented Programming
|
67 | 131 |
|
68 | 132 | The remaining style of programming we will discuss here is called Object-Oriented Programming.
|
@@ -113,11 +177,11 @@ months
|
113 | 177 | .forEach(month => month.toConsole());
|
114 | 178 | ```
|
115 | 179 |
|
116 |
| -### The prototype chain |
| 180 | +### The Prototype Chain |
117 | 181 |
|
118 | 182 | 
|
119 | 183 |
|
120 |
| -### Pre-ES6 constructor functions and prototypes |
| 184 | +### Pre-ES6 Constructor Functions and Prototypes |
121 | 185 |
|
122 | 186 | ```js
|
123 | 187 | function Month(name, days) {
|
@@ -161,7 +225,7 @@ months
|
161 | 225 | .forEach(month => month.toConsole());
|
162 | 226 | ```
|
163 | 227 |
|
164 |
| -### Bonus: Array.prototype.map & Array.prototype.filter implementations |
| 228 | +### Bonus: Array.prototype.map & Array.prototype.filter Implementations |
165 | 229 |
|
166 | 230 | ```js
|
167 | 231 | Array.prototype.myMap = function (mapFn) {
|
|
0 commit comments