lundi 24 décembre 2018

Javascript: Which design better or approach could fit on those iterative methods?

Sorry for this not-specific but general title for post but I couldn't set it specifically while usage of Design Patterns are on a theoretical base.

I would like to share couple of code-snippets which shows a very similar usage but tiny changes. I've tried to make a easy-maintainable code block but I couldn't achieve the aim.

let preLabel = self.fieldLabel,
               asteriksLabel = preLabel += " *";

// Initialise way of function
function getLabel(self) {
    setAsteriks();
}

function hideLabel(self) {
    setAsteriks('EmptyText');
}

//and the function itself which I'm looking a Design Pattern;
function setAsteriks(emptyText = false) {
    if (emptyText) {
        if (!self.allowBlank) {
            self.setEmptyText(asteriksLabel);
        } else {
            self.setEmptyText(preLabel);
        }
    } else {
        if (!self.allowBlank) {
            if (self.fieldLabel.slice(-1) !== "*") {
                self.setFieldLabel(asteriksLabel);
            }
        } else {
            self.setFieldLabel(preLabel);
        }
    }
}

As you'll notice setAsteriks method queries emptyText parameter and through it decided to use setEmptyText or setFieldLabel.

Thanks for any advice.

Aucun commentaire:

Enregistrer un commentaire