mercredi 12 janvier 2022

Is it valid to have a JavaScript function that does nothing?

I'm working three texts boxes that get data and display them in a jQuery autocomplete drop down. Since JavaScript is rather flexible with it's objects and does not have interfaces. I made an API call object that then takes in an object of functions that gets spread into it's self.

export function ApiCall(baseUrl, domain, path, options = {}) {
    return {
        baseUrl: baseUrl,
        domain: domain,
        path: path,
        getData: function () {
            return $.ajax({
                url: this.fullUrl,
                dataType: "json",
                data: this.data,
                success: function(data) { return data }
            });
        },
        ...options
    }
}

This allowed me to get rid of the majority of if blocks since I no longer had to check for html IDs. I have however ran into two issues with this. Some of the objects need to have an empty function that does nothing while others have that function do something.

addError: function () {
                        addError(this.title)
                    },

and others

addError: () => { }

This is growing some and I'm worried that the objects might have some implanting them and others not. As is now the code is working great and it has cleared up many if blocks that do different things depending on what html ID is being passed around and just runs the function on the object. Is this valid? Any better ways to implement this? Any ways to clear this up so it's more readable and less confusing?

Aucun commentaire:

Enregistrer un commentaire