-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-criando-classes.js
More file actions
33 lines (24 loc) · 874 Bytes
/
01-criando-classes.js
File metadata and controls
33 lines (24 loc) · 874 Bytes
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
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Olá, meu nome é ${this.name}`
}
}
const user = new Person('Kauan Rodrigues', 17)
console.log(user.greet());
// OBS:
// Embora você esteja usando a sintaxe de classe (class), o JavaScript funciona com funções construtoras e prototipagem por baixo dos panos.
// A palavra-chave class é apenas uma forma mais moderna e legível de escrever funções construtoras e usar o prototype. Internamente, o código acima é equivalente a algo assim
// Por baixo dos panos o código de cima faz isso:
function Person2(name, age) {
this.name = name;
this.age = age;
}
Person2.prototype.greet = function () {
return `Olá, meu nome é ${this.name}`
}
const user2 = new Person2('Kauan Rodrigues', 17)
console.log(user2.greet());