-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-classes-in-js.js
107 lines (91 loc) · 2.71 KB
/
04-classes-in-js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Prototypes with Classes sintaxis.
// we use the keyword 'class' to use this sintaxis.
class Student {
// the constructor is the methods that is executed once a instance is request.
constructor (name, age, approvedCourses) {
this.name = name
this.age = age
this.approvedCourses = approvedCourses
//the properties works pretty much the same.
}
//the methods are inside the class but out of the constructor
approveCourse(newCourse) {
this.approvedCourses.push(newCourse)
//this. keeps making reference to this object.
}
}
// It is just a modern sintaxis but it keeps the same prototypes principles.
// Creation of an instance from Student Class (Prototype)
const miguelito = new Student(
'Miguel',
28,
[
'Business Analysis for Data Science Course',
'Principles of Data Visualization for BI Course'
]
)
miguelito.approveCourse('Tableau Course')
console.log(miguelito);
/*
Student {
name: 'Miguel',
age: 28,
approvedCourses: [
'Business Analysis for Data Science Course',
'Principles of Data Visualization for BI Course',
'Tableau Course'
]
}
*/
// What if we have many parameters?
/*
We can use the RORO pattern.
RORO => Receive an object, return an object.
-It helps us when we have many parameters in our constructor,
since when using an object, it is easy to deconstruct to obtain
what we want, making the call to the keys without worrying about
the order.
-It is necessary to keep in mind that it must send the
information in object form.
-A plus would be to add values by default in the classes,
in case that when instantiating they are not provided.
source: https://www.freecodecamp.org/news/elegant-patterns-in-modern-javascript-roro-be01e7669cbd/
*/
class Student2 {
//here we set an object as a parameter with default assigns to the variables.
constructor ({
name,
age,
approvedCourses = [],
email = 'example@mail.com'} = {}) {
this.name = name
this.age = age
this.email = email
this.approvedCourses = approvedCourses
}
approveCourse(newCourse) {
this.approvedCourses.push(newCourse)
}
}
const nath = new Student2({
name:'Natalia',
age:24,
approvedCourses:[
'Business Analysis for Data Science Course',
'Principles of Data Visualization for BI Course'
]
})
nath.approveCourse('Tableau Course')
console.log(nath);
/*
Student2 {
name: 'Natalia',
age: 28,
email: 'example@mail.com',
approvedCourses: [
'Business Analysis for Data Science Course',
'Principles of Data Visualization for BI Course',
'Tableau Course'
]
}
*/