-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-playground-overwriting-inherited-methods.js
117 lines (108 loc) · 2.72 KB
/
13-playground-overwriting-inherited-methods.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
106
107
108
109
110
111
112
113
114
115
116
117
/*
Within this challenge you will find the "Student" and "Comment"
classes with a code base, remember to check them out
in the filesystem.
Your challenge consists of:
Make the "TeacherStudent" class inherit from "Student".
Add an Array property called "skills" to the "TeacherStudent" class.
Override the method "publicarComentario" inside "TeacherStudent".
You must add the array of skills in the "studentRole" property
of the "Comment" class.
The string should look like this "profesor de skill1,skill2".
Input:
const skills = ["js", "python", "mongodb"]
const props = {
name: "Erik Ochoa",
username: "elyager"
}
const profesor = new TeacherStudent(props, skills)
profesor.publicarComentario('Mi primer comentario')
Output:
{
"studentName":"Erik Ochoa (profesor de js,python,mongodb)",
"likes":"0 likes",
"content":"Mi primer comentario"
}
*/
class Comment {
constructor({
content,
studentName,
studentRole = "estudiante",
}) {
this.content = content;
this.studentName = studentName;
this.studentRole = studentRole;
this.likes = 0;
}
publicar() {
const comentario = {
studentName: this.studentName + " (" + this.studentRole + ")",
likes: this.likes + " likes",
content: this.content
}
return comentario
}
}
class Student {
constructor({
name,
email,
username,
twitter = undefined,
instagram = undefined,
facebook = undefined,
approvedCourses = [],
learningPaths = [],
}) {
this.name = name;
this.email = email;
this.username = username;
this.socialMedia = {
twitter,
instagram,
facebook,
};
this.approvedCourses = approvedCourses;
this.learningPaths = learningPaths;
}
publicarComentario(commentContent) {
const comment = new Comment({
content: commentContent,
studentName: this.name,
});
return comment.publicar();
}
}
//inheritance application
class TeacherStudent extends Student {
constructor(props, skills =[]) {
super(props)
this.skills = skills
}
// polymorphism application
publicarComentario(commentContent) {
const role = `profesor de ${this.skills.join(',')}`
const comment = new Comment({
content: commentContent,
studentName: this.name,
studentRole: role,
});
return comment.publicar();
}
}
// Test with instances
const skills = ["js", "python", "mongodb"]
const props = {
name: "Erik Ochoa",
username: "elyager"
}
const profesor = new TeacherStudent(props, skills)
console.log(profesor.publicarComentario('Mi primer comentario'));
/*
{
studentName: 'Erik Ochoa (profesor de js,python,mongodb)',
likes: '0 likes',
content: 'Mi primer comentario'
}
*/