I cannot understand this Typeerror : is not a constructor

Asked

Viewed 89 times

0

When I click the button salvage it performs the expected behavior (creates a training instance, fills the instance with the value of input and saved in an object instance list calendario) and it is possible to use the button salvage as many times as you want.

However, after I press the button show and try to squeeze again the salvage get the bug:

Typeerror: treino is not a constructor

But this error should not occur the first time I tighten the salvage?

Code:

        class treino {
            treino(dia = "",mes="",ano="",tipo="",url=""){
                this.dia = dia;
                this.mes = mes;
                this.ano = ano;
                this.tipo = tipo;
                this.url = "#";
            }
            
            
            setDados(){
                this.dia = document.getElementsByName("treino-dia")[0].value;
                this.mes = document.getElementsByName("treino-mes")[0].value;
                this.ano = document.getElementsByName("treino-ano")[0].value;
                this.tipo = document.getElementsByName("treino-tipo")[0].value;
                this.url = "#";
            }
            
            mostrar(){
                return `${this.dia}\/${this.mes}\/${this.ano}\ntipo: ${this.tipo}\nurl: ${this.url}`;
            }
        }

        class calendario {
            constructor(){
                this.lista = [];
            }
            
            add(treino){
                this.lista.push(treino);
            }
            
            mostrar(){
                for(treino of this.lista){
                console.log(treino.mostrar());
                }
            }

        }

        function salvar(){
            let t = new treino();
            t.setDados();
            c.add(t);
            console.log(c);
        }

        function mostrarCa(){
            c.mostrar();
        }
        let c = new calendario();
<html>
<head>
<meta charset="UTF-8">
    <title>Classes e instancias</title>
</head>
<body translate="no">
    <h1>Registrar Treino</h1>
    <input type="number" max="31" min="1" name="treino-dia" placeholder="Dia" value="1">
    <input type="number" max="12" min="1" name="treino-mes" placeholder="Mês" value="1">
    <input type="number" min="2020" name="treino-ano" placeholder="Ano" value="1">
    <br>
    <input type="text" name="treino-tipo" placeholder="Treino" value="treino">
    <button onclick="salvar()">Salvar</button>
    <button onclick="mostrarCa()">Mostrar</button>
</body>
</html>

1 answer

1

The error is in your loop, inside the display method:

mostrar(){
  for(treino of this.lista){
    console.log(treino.mostrar());
  }
}

Note that in your loop, you do not declare the training variable, and this ends up generating your mistakes, to correct just declare the training variable, with var, let or const:

mostrar(){
  for(const treino of this.lista){
    console.log(treino.mostrar());
  }
}

As you will not change this variable, it is very common for it to be declared as const, some lints even verify this.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

  • Although actually missing the variable declaration, unfortunately it was not enough to resolve. To be more precise the code error occurs on the line Let t = new workout(); After pressing the show button. Before that it works normally.

  • Did not correct? And what error was presented after the variable declaration? I tested and the statement solved the problem: https://repl.it/repls/HomelyLikelyVirtualmemory

Browser other questions tagged

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