|
| 1 | +# Object-Oriented Programming & Classes |
| 2 | + |
| 3 | +### Object literals and Functional Programming |
| 4 | +```js |
| 5 | +const months = [ |
| 6 | + { name: 'January', days: 31 }, |
| 7 | + { name: 'February', days: 28 }, |
| 8 | + { name: 'March', days: 31 }, |
| 9 | + { name: 'April', days: 30 }, |
| 10 | + { name: 'May', days: 31 }, |
| 11 | + { name: 'June', days: 30 }, |
| 12 | + { name: 'July', days: 31 }, |
| 13 | + { name: 'August', days: 31 }, |
| 14 | + { name: 'September', days: 30 }, |
| 15 | + { name: 'October', days: 31 }, |
| 16 | + { name: 'November', days: 30 }, |
| 17 | + { name: 'December', days: 31 } |
| 18 | +]; |
| 19 | + |
| 20 | +const isLongMonth = month => month.days === 31; |
| 21 | +const toString = month => month.name + ' has ' + month.days + ' days.'; |
| 22 | + |
| 23 | +const longMonths = months.filter(isLongMonth); |
| 24 | +const strings = longMonths.map(toString); |
| 25 | +strings.forEach(string => console.log(string)); |
| 26 | +``` |
| 27 | + |
| 28 | +### ES6 Classes and Object-Oriented Programming |
| 29 | + |
| 30 | +```js |
| 31 | +class Month { |
| 32 | + constructor(name, days) { |
| 33 | + this.name = name; |
| 34 | + this.days = days; |
| 35 | + } |
| 36 | + |
| 37 | + hasDays(days) { |
| 38 | + return this.days === days; |
| 39 | + } |
| 40 | + |
| 41 | + isLongMonth() { |
| 42 | + return this.hasDays(31); |
| 43 | + } |
| 44 | + |
| 45 | + toString() { |
| 46 | + return `${this.name} has ${this.days} days.`; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const months = [ |
| 51 | + new Month('January', 31), |
| 52 | + new Month('February', 28), |
| 53 | + new Month('March', 31), |
| 54 | + new Month('April', 30), |
| 55 | + new Month('May', 31), |
| 56 | + new Month('June', 30), |
| 57 | + new Month('July', 31), |
| 58 | + new Month('August', 31), |
| 59 | + new Month('September', 30), |
| 60 | + new Month('October', 31), |
| 61 | + new Month('November', 30), |
| 62 | + new Month('December', 31) |
| 63 | +]; |
| 64 | + |
| 65 | +const longMonths = months.filter(month => month.isLongMonth()); |
| 66 | +longMonths.forEach(month => console.log(month.toString())); |
| 67 | +``` |
| 68 | + |
| 69 | +### The prototype chain |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +### Pre-ES6 constructor functions and prototypes |
| 74 | + |
| 75 | +```js |
| 76 | +function Month(name, days) { |
| 77 | + this.name = name; |
| 78 | + this.days = days; |
| 79 | +} |
| 80 | + |
| 81 | +Month.prototype.hasDays = function (days) { |
| 82 | + return this.days === days; |
| 83 | +}; |
| 84 | + |
| 85 | +Month.prototype.isLongMonth = function () { |
| 86 | + return this.hasDays(31); |
| 87 | +}; |
| 88 | + |
| 89 | +Month.prototype.toString = function () { |
| 90 | + return `${this.name} has ${this.days} days.`; |
| 91 | +}; |
| 92 | + |
| 93 | +const months = [ |
| 94 | + new Month('January', 31), |
| 95 | + new Month('February', 28), |
| 96 | + new Month('March', 31), |
| 97 | + new Month('April', 30), |
| 98 | + new Month('May', 31), |
| 99 | + new Month('June', 30), |
| 100 | + new Month('July', 31), |
| 101 | + new Month('August', 31), |
| 102 | + new Month('September', 30), |
| 103 | + new Month('October', 31), |
| 104 | + new Month('November', 30), |
| 105 | + new Month('December', 31) |
| 106 | +]; |
| 107 | + |
| 108 | +const longMonths = months.filter(month => month.isLongMonth()); |
| 109 | +longMonths.forEach(month => console.log(month.toString())); |
| 110 | +``` |
0 commit comments