-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-polymorphism.js
102 lines (97 loc) · 2.72 KB
/
12-polymorphism.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
/*
- The Polymorphism in JS:
It allows our subclasses to change or override the behaviors of
the methods and attributes of the parent prototype,
of the parent class. Although it inherits the properties,
polymorphism allows to change its behavior.
-Inclusion Polymorphism (JS): The ability to completely
redefine the method of a superclass in a subclass.
*/
class Comment {
constructor({
content,
studentName,
studentRole = 'student',
}){
this.content = content
this.studentName = studentName
this. studentRole = studentRole
this.likes = 0
}
post() {
console.log(`${this.studentName} (${this.studentRole})`);
console.log(`${this.likes} likes`);
console.log(this.content);
}
}
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
}
// Creating an instance from a prototype method
postComment(commentContent) {
const comment = new Comment({
content: commentContent,
studentName: this.name,
})
comment.post()
}
}
/* Applying inheritance creating a Subclass based on the Class Student,
using the keyword super we get the properties from the parent class*/
class TeacherStudent extends Student {
constructor(props) {
super(props)
}
// this method is added in this Subclass
approveCourse(newCourse) {
this.approvedCourses.push(newCourse)
}
/* However, this class is applying polymorphism by redefining the
method created in the parent class.
*/
postComment(commentContent) {
const comment = new Comment({
content: commentContent,
studentName: this.name,
studentRole: 'professor',
})
comment.post()
}
}
const freddy = new TeacherStudent ({
name: 'Freddy Vega',
username: 'freddier',
email: 'f@gep.com',
instagram: 'freddier',
})
console.log(freddy);
/*TeacherStudent {
name: 'Freddy Vega',
email: 'f@gep.com',
username: 'freddier',
socialMedia: { twitter: undefined, instagram: 'freddier', facebook: undefined },
approvedCourses: [],
learningPaths: [] }*/
freddy.postComment('Excelente, me encanta esta clase :)')
/* Freddy Vega (professor)
0 likes
Excelente, me encanta esta clase :) */