2
Hello, everybody.
I have the Students, Students and Students classes. These last two classes inherit all methods and attributes of the Student class (name, enrollment, sex, grades, display(), assignNota(), lerNota(), calculationMedia(), and add new ones to them.
There is how to create these last two classes so that the Student class is their parent?
var Estudante = function (nome, matricula, sexo) {
this.nome = nome;
this.matricula = matricula;
this.sexo = sexo;
var notas = [];
this.exibir = function () {
return "oi";
};
this.atribuirNota = function (n) {
nota.push(n);
};
this.lerNota = function (index) {
return [index-1];
};
calcularMedia = function () {
var x = (notas.length);
var soma = 0;
for (i=0; i<(x - 1); i++ ){
soma += notas[i];
}
var media = soma / x;
return media;
}
}
var EstudanteMonitor = Object.create(Estudante);
EstudanteMonitor.prototype.constructor = EstudanteMonitor;
var e1 = new EstudanteMonitor("oi", 2, 3);
console.log(e1.nome);
An example of how I’m trying to do.
Thanks for the answer, but can’t he inherit the attributes too? Example: Function Student Monitor(name, matricula, sex) { } // Here’s the inheritance, you say the Student Prototype is Student Student Monitor.prototype = new Student(); Student Monitor.prototype.constructor = Student Monitor; var e = new Student Monitor(1, 2, 3); console.log(e.name); //return '1', but returns Undefined.
– Chosmos
But he inherits the attributes, what he does not inherit is the constructor, you have to define the same constructor in
function EstudanteMonitor
. To confirm, create an attribute in the Student class that is not assigned by the constructor,this.idade = 18;
for example, and try at the end to use aconsole.log(e.idade);
to see the result.– Ricardo Pontual
If I understood, putting the parameters in the function should not work?
var Estudante = function (nome, matricula, sexo) {
 //... 
}

function EstudanteMonitor(nome, matricula, sexo) {
 this.nome;
 this.matricula = matricula;
 this.sexo = sexo;

}
// Aqui vai a herança, você diz que o prototype de EstudanteMonitor é Estudante
EstudanteMonitor.prototype = new Estudante();
EstudanteMonitor.prototype.constructor = EstudanteMonitor;

var e = new EstudanteMonitor("carlos", 2, 3);
console.log(e.nome);
//Continues to return Undefined.– Chosmos
Almost that, but you have to call the parent class using it in the builder:
Estudante.call( this, nome, matricula, sexo );
See my answer, edited with the example– Ricardo Pontual
I did it! Thank you very much, young man (and sorry for so many questions)!
– Chosmos
need only ask
– Ricardo Pontual