What is the best way to do a javascript with standardized messages?

Asked

Viewed 54 times

-2

I would like to make some script that returns something like

MENSAGEM_SUCESSO = 'Registro inserido com sucesso';
MENSAGEM_ERRO = 'Problemas ao inserir o registro!';

Soon in some Function call these "constants":

function Salvar() {
   // se salvou com sucesso
   Alerta(MENSAGEM_SUCESSO, 'Mensagem'); // função hipotetica que exibe um alert em bootstrap no html
}

I would like to parameterize the messages in a file js

What’s better? const? Let? or just a string file.

I need some guidance.

  • But const does not solve your problem? Why not?

  • If it does, what is the doubt? It’s not clear what you need to know. If it is only the "best way", can be considered as based on opinions, at least if you do not list all the requirements that the solution should meet, in which case who knows can evaluate the best.

1 answer

2


Better than that, I recommend you create methods to represent your messages.

For example:

function Sucesso(nomeRegistro){
  alert(`${nomeRegistro} inserido(a) com sucesso!`);
}

function Falha(nomeRegistro){
  alert(`Problemas ao inserir ${nomeRegistro}!`);
}

if (true) {
  Sucesso("Produto");
}
else {
  Falha("Produto");
}

I hope I gave a light.

Browser other questions tagged

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