This helps a lot with readability , but I continue to have issues with reusabilty . To be more specific, the difficulty is in two areas:
-
Receiving parameters . A lot of times I will have a search screen with multiple input fields and a button that calls the javascript search function. I have to either put a bunch of code in the onclick of the button to retrieve and then martial the values from the input fields into the function call, or I have to hardcode the HTML input field names/IDs so that I can subsequently retrieve them with Javascript. The solution I’ve settled on for this is to pass the field names/IDs into the function, which it then uses to retrieve the values from the input fields. This is simple but really seems improper.
-
Returning values . The effect of most Javascript calls tends to be one in which some visual on the screen changes directly, or as a result of another action performed in the call. Reusability is toast when I put these screen-altering effects at the end of a function. For example, after a search is completed I need to display the results on the screen.
// Example
function ourReusableFunction() {
console.log(“Heyya, World”);
}
ourReusableFunction();
// Only change code below this line
function reusableFunction() {
console.log(“Hi World”);
}
reusableFunction();