-
Notifications
You must be signed in to change notification settings - Fork 543
/
22-classes.js
193 lines (130 loc) · 2.83 KB
/
22-classes.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Clase 37 - Clases
Vídeo: https://youtu.be/1glVfFxj8a4?t=16864
*/
// Clases
class Person {
constructor(name, age, alias) {
this.name = name
this.age = age
this.alias = alias
}
}
// Sintaxis
let person = new Person("Brais", 37, "MoureDev")
let person2 = new Person("Brais", 37, "MoureDev")
console.log(person)
console.log(person2)
console.log(typeof person)
// Valores por defecto
class DefaultPerson {
constructor(name = "Sin nombre", age = 0, alias = "Sin alias") {
this.name = name
this.age = age
this.alias = alias
}
}
let person3 = new DefaultPerson("Brais", 37)
console.log(person3)
// Acceso a propiedades
console.log(person3.alias)
console.log(person3["alias"])
person3.alias = "MoureDev"
console.log(person3.alias)
// Funciones en clases
class PersonWithMethod {
constructor(name, age, alias) {
this.name = name
this.age = age
this.alias = alias
}
walk() {
console.log("La persona camina.")
}
}
let person4 = new PersonWithMethod("Brais", 37, "MoureDev")
person4.walk()
// Propiedades privadas
class PrivatePerson {
#bank
constructor(name, age, alias, bank) {
this.name = name
this.age = age
this.alias = alias
this.#bank = bank
}
pay() {
this.#bank
}
}
let person5 = new PrivatePerson("Brais", 37, "MoureDev", "IBAN123456789")
// No podemos acceder
// console.log(person5.bank)
// person5.bank = "new IBAN123456789" // bank no es #bank
console.log(person5)
// Getters y Setters
class GetSetPerson {
#name
#age
#alias
#bank
constructor(name, age, alias, bank) {
this.#name = name
this.#age = age
this.#alias = alias
this.#bank = bank
}
get name() {
return this.#name
}
set bank(bank) {
this.#bank = bank
}
}
person6 = new GetSetPerson("Brais", 37, "MoureDev", "IBAN123456789")
console.log(person6)
console.log(person6.name)
person6.bank = "new IBAN123456789"
/*
Clase 38 - Herencia de clases
Vídeo: https://youtu.be/1glVfFxj8a4?t=17999
*/
// Herencia
class Animal {
constructor(name) {
this.name = name
}
sound() {
console.log("El animal emite un sonido genérico")
}
}
class Dog extends Animal {
sound() {
console.log("Guau!")
}
run() {
console.log("El perro corre")
}
}
class Fish extends Animal {
constructor(name, size) {
super(name)
this.size = size
}
swim() {
console.log("El pez nada")
}
}
let myDog = new Dog("MoureDog")
myDog.run()
myDog.sound()
let myFish = new Fish("MoureFish", 10)
myFish.swim()
myFish.sound()
// Métodos estáticos
class MathOperations {
static sum(a, b) {
return a + b
}
}
console.log(MathOperations.sum(5, 10))