method rebuilder class javascript?

2

I'm planning a class letter:

class Carta {
    constructor (el) {
        // propriedades imutaveis
        this.el = el;
    }
    // metodos
    reconstrutor () {
        // propriedades mutaveis
    }
    isManilha () {

    }
    desenhar () {

    }
    apagar () {

    }
    jogar () {

    }
}

Inside the constructor I keep el which is an html element. And a card game so every round I could delete the old objects and instantiate new ones with new properties, but the el would have to be passed again. So would it be more correct to use a rebuilding method to just change the required properties or would it be a gambiarra?

    
asked by anonymous 24.08.2017 / 22:46

1 answer

0

The idea of class inheritance does what you're looking for. There are two alternatives to having this "rebuilder":

#1 - If it is only el that changes, or other parameters that can be passed in variables then it uses the same class but passes only new arguments:

var A = new Carta('A', 'Joker', etc...);

#2 - If you want to have different functionality, but using much of the code from another class you can use extends , which is basically a copy of the other class where you can overwrite methods:

classe Joker extends Carta {
    constructor(el){
        super(el); // <- aqui chamas o construtor de Carta passando 'el'.
    }

    desenhar () {
        // aqui podes escrever lógica que é só do Joker
        // os outros métodos serão os mesmos, mas este vai substituir
        // o método desenhar da classe Carta
    }

}
    
25.08.2017 / 06:17