3
As far as I knew, use //
to comment on a line and /*...*/
was the same thing, with the difference that the second covered more than one line if needed.
Is there ANY difference between doing
// var a = 'teste'
and
/* var a = 'teste' */
?
I wonder because I came across a strange case, where I commented code with a problem inside a template string (which created a script tag with a code with a syntax error inside it) and as I commented the line with problem using the /* */
I did not receive syntax error, but when commenting the same line with //
, gave me syntax error while running.
Follow the code where I had the problem:
$('#dadosBeneficiario').html(`
<script type="application/javascript">
function dropDown(node) {
var drop = $(node).next();
drop.slideToggle('slow');
}
function toggleObs(botao) {
var campo = $(botao).prev();
if (campo.val() !== '') {
if($('#descricaoOpcional').val() !== '') {
/* $('#descricaoOpcional').val($('#descricaoOpcional').val() + "\n______________________________________________________\n" + campo.val()); */
$(botao).hide();
} else {
$('#descricaoOpcional').val($('#descricaoOpcional').val() + campo.val());
$(botao).hide();
}
} else if ($(botao).val() === "Salvar") {
return;
}
campo.toggle('fast');
$(botao).val('Salvar');
}
</script>`);
When commenting on the line this way, the code ignored the line and ran. But when commenting with double bar, I received syntax error:
// $('#descricaoOpcional').val($('#descricaoOpcional').val() + "\n______________________________________________________\n" + campo.val());
Why did this occur?
Maniero, I appreciate the information, but I still can’t figure out why the code gave me a syntax error while commenting on double bars. I made a mistake commenting? What happened?
– Máttheus Spoo
As I wrote we have no way of knowing, you just posted right and not wrong.
– Maniero
I edited the question with the form I commented with double bar, sorry.
– Máttheus Spoo
Without context, so it is obvious that there is no error, within a context could give, but only in it would give to see the error. You didn’t even post the mistake, it might not even be what you’re talking about.
– Maniero
I believe the error was to be inside the template string, when I try outside I get no error. The error was a normal syntax error stating that there was an unexpected character on a line
– Máttheus Spoo
Now that I’ve seen the huge gambiarra there. It’s not even a code, it’s a text that will then be injected by jQuery. Well, almost all use of jQuery is wrong. Today it is almost unnecessary, and what people use is to type less, which in itself is a mistake. There’s an easy and simple way to do it and a crazy way, that’s the crazy way, that can give all kinds of problems. To tell you the truth is luck works even without comment.
– Maniero
I had to do this because this html div was ignoring external code for some reason that even the full programmers here at my company couldn’t explain. So I had to inject the JS into the div.
– Máttheus Spoo
The solution to fix one gambiarra is never to create another.
– Maniero
The system where I work has so much gambiarra within gambiarra within another gambiarra since I joined the company that to tidy everything would be easier to develop again from scratch. Anyway, to finish this, in practice it is no difference to comment on a line of the two different ways as I asked at the beginning, at the time of reading, right?
– Máttheus Spoo
Under normal conditions, no.
– Maniero