method reconstructor class javascript?

Asked

Viewed 27 times

2

I’m planning a letter class:

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

    }
    desenhar () {

    }
    apagar () {

    }
    jogar () {

    }
}

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

  • I don’t know what you want, but I imagine it’s at least inventive.

  • was exactly what I wanted to know, to know if I’m on the right track or not and I’ve seen that not, what to have php as the first language.

  • If the letter object will be "reset" - a new card, I think I could use only the constructor. Unless only properties of the same letter will be changed, for example, it be manila at a certain time and not be more at another time.

1 answer

0


The idea of inheritance in classes does what you seek. You see that you have this "reconstructor" you have two alternatives:

#1- If only el that changes, or other parameters that can be passed in variables then 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 another class code you can use extends, that at the bottom is a copy of the other class where you can over-write 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
    }

}

Browser other questions tagged

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