Herança ES5 vs ES6 | super/extends
//---- ES6
class Pessoa {
constructor(nome) {
this._nome = nome;
}
falar(mensagem) {
console.log(`${this._nome}: ${mensagem}`);
}
}
class Funcionario extends Pessoa {
constructor(nome) {
super(nome);
}
trabalha() {
console.log(`${this._nome} está trabalhando.`);
}
}
let func = new Funcionario('Julia');
func.falar('Bom dia');
func.trabalha();
//---- ES5
function Pessoa(_nome) {
this._nome = _nome;
}
Pessoa.prototype.falar = function (mensagem) {
console.log(this._nome + ': ' + mensagem);
}
function Funcionario(_nome) {
// super
Pessoa.call(this, _nome);
}
// Funcionario extends Pessoa
Funcionario.prototype = Object.create(Pessoa.prototype);
Funcionario.prototype.constructor = Funcionario;
//Metodos Funcionario
Funcionario.prototype.trabalha = function () {
console.log(this._nome + ' está trabalhando.');
}
var func = new Funcionario('Julia');
func.falar('Bom dia');
func.trabalha();