-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.js
42 lines (34 loc) · 909 Bytes
/
factory.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
// Factory function (Função fábrica)
// Constructor function (Função construtora)
function criaPessoa(nome, sobrenome, a, p) {
return {
nome,
sobrenome,
// Getter
get nomeCompleto() {
return `${this.nome} ${this.sobrenome}`;
},
// Setter
set nomeCompleto(valor) {
valor = valor.split(' ');
this.nome = valor.shift();
this.sobrenome = valor.join(' ');
},
fala(assunto = 'falando sobre NADA') {
return `${this.nome} está ${assunto}.`;
},
altura: a,
peso: p,
// Getter
get imc() {
const indice = this.peso / (this.altura ** 2);
return indice.toFixed(2);
}
};
}
const p1 = criaPessoa('Ederson', 'Melo', 1.8, 80);
const p2 = criaPessoa('Ederson', 'de Maria', 1.90, 57);
const p3 = criaPessoa('Ederson', 'de maria melo', 1.5, 110);
console.log(p1.imc);
console.log(p2.imc);
console.log(p3.imc);