Estou revisitando o frontend do meu projeto. Inicialmente, apesar de saber que o desempenho e a legibilidade do código estavam fora do padrão esperado, o código que funcionava era esse:
var currentContent = $(this).html().trim();
// For some god forsaken reason, sometimes currentContent will
// contain a <br> value instead of a simple empty string.
// I do not understand WHY it happens but this accounts for it...
// for now :)
if (currentContent === "<br>" || currentContent === "") {
if ($(this).hasClass("article-first-paragraph")) {
$(this).html(defaultParagraphContent);
} else if ($(this).hasClass("title")) {
$(this).html(defaultTitleContent);
} else {
$(this).html("New paragraph");
}
}
}```
Por isso, decidi refatorá-lo com Object Literals:
``` const defaultContent = {
title: "Title",
firstParagraph: "Share your thoughts...",
newParagraph: "New paragraph"
}
// Adds default text if the user does not make any changes
// gets called on focusout event
function focusOut() {
var currentContent = $(this).html().trim();
// For some god forsaken reason, sometimes currentContent will
// contain a <br> value instead of a simple empty string.
// I do not understand WHY it happens but this accounts for it
if (currentContent === "<br>" || currentContent === "") {
const textObjectClass = $(this).attr("class").slice(9);
const newObjectContent = defaultContent[textObjectClass];
$(this).html(newObjectContent);
}
}```
Vou terminar a dúvida na próxima mensagem
