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

Skip to content

Commit 399ba3f

Browse files
committed
added fundamental for OOP and ES6 classes
1 parent fb22cae commit 399ba3f

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

VSCodeTips/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ We recommend that you apply two changes to the default settings of VSCode to hel
5050
![settings-btn](assets/settings.png)
5151

5252
2. From the menu, select **Settings**.
53-
3. In the `USER SETTINGS` tab in the right-hand half of the screen add the two lines indicated below between the curly braces:
53+
3. In the `USER SETTINGS` tab in the right-hand half of the screen add the three lines indicated below between the curly braces:
5454

5555
```json
5656
/// Place your settings in this file to overwrite the default settings
5757
{
5858
"editor.formatOnType": true,
59-
"editor.formatOnPaste": true
59+
"editor.formatOnPaste": true,
60+
"editor.formatOnSave": true
6061
}
6162
```
6263

fundamentals/assets/prototype.png

40.1 KB
Loading

fundamentals/map_filter.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ accumulator = reducerFn(accumulator, this[i]);
130130

131131
From this line we can define the reducer function as a function that takes an accumulator value and the current array element and returns a new accumulator value.
132132

133-
The whole process is visualised in the figure below (the term _bucket_ was used here to represent the accumulator).
134-
135-
![image](assets/reduce-bucket.png)
136-
137133
The **reduce()** method is the most flexible of the map/filter/reduce triplet. In fact, it is possible to rewrite **map()** and **filter** using **reduce()**.
138134

139135
### Using reduce() to filter

fundamentals/oop_classes.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
![prototype](assets/prototype.png)
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

Comments
 (0)