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>
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.
– Gustavo Souza
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
– Daniel Mendes