How to create a class from another class? (Hierarchy)

Asked

Viewed 113 times

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.

2 answers

2

The modern way to do this is with classes implemented in the ES6/ES2015 version of Javascript/Ecmascript. This is already available in almost all browsers. In this case the syntax is:

class EstudanteMonitor extends Estudante {

and would look like this:

class Estudante {
    constructor(nome, matricula, sexo) {
        this.nome = nome;
        this.matricula = matricula;
        this.sexo = sexo;
        this.notas = [];
    }

    exibir() {
        return 'Nome do estudante: ' + this.nome;
    }

    atribuirNota(n) {
        this.notas.push(n);
    }

    lerNota(index) {
        return this.notas[index - 1];
    }

    calcularMedia() {
        var x = (this.notas.length);
        var soma = 0;
        for (i = 0; i < (x - 1); i++) {
            soma += this.notas[i];
        }
        var media = soma / x;
        return media;
    }
}

class EstudanteMonitor extends Estudante {
    exibir() {
        return 'Nome do estudante monitor: ' + this.nome;
    }
};

var e1 = new Estudante("João", 2, 3);
console.log(e1.exibir()); // "Nome do estudante: João
var em1 = new EstudanteMonitor("João", 2, 3);
console.log(em1.exibir()); // "Nome do estudante monitor: João"

jsFiddle: https://jsfiddle.net/py0r32xk/

0


Yes, you must declare so:

function EstudanteMonitor() {

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

To inherit also the builder, you must do so:

function EstudanteMonitor(nome, matricula, sexo) {
   Estudante.call( this, nome, matricula, sexo );
}

Note that Student Monitor does not have a definition for exibir(), but it will work because of inheritance;

See more here: http://loopinfinito.com.br/2012/05/04/heranca-em-javascript-parte-1/

  • 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.

  • 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 a console.log(e.idade); to see the result.

  • If I understood, putting the parameters in the function should not work? var Estudante = function (nome, matricula, sexo) {&#xA; //... &#xA;}&#xA;&#xA;function EstudanteMonitor(nome, matricula, sexo) {&#xA; this.nome;&#xA; this.matricula = matricula;&#xA; this.sexo = sexo;&#xA;&#xA;}&#xA;// Aqui vai a herança, você diz que o prototype de EstudanteMonitor é Estudante&#xA;EstudanteMonitor.prototype = new Estudante();&#xA;EstudanteMonitor.prototype.constructor = EstudanteMonitor;&#xA;&#xA;var e = new EstudanteMonitor("carlos", 2, 3);&#xA;console.log(e.nome);//Continues to return Undefined.

  • 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

  • I did it! Thank you very much, young man (and sorry for so many questions)!

  • need only ask

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.