I'm working on a JavaScript user script to assist my team in making assembling support tickets on a web interface more efficient. In doing so, I'm finding myself making a lot of calls to DOM API to gather data, e.g., with event listeners and user-triggered calls.
In an attempt to make my script more concise, I have made a few "alias" functions, such as below.
I am wondering if doing something like this would make the code more difficult to maintain, in the case of some other person (or myself in 6 months) having to add to or change the code later on, or if it would be beneficial, in cutting out unneeded repetition of code like document.getElementById("foo").addEventListener("change", doBar)
.
Question
While this may be somewhat opinion-based, I want to inquire: Is aliasing [well-known, standard] repetitive function calls likely to be an obstacle to keeping the code clear and relatively easy to maintain?
For a bit more background, this will eventually be a template helper to automate repetitive tasks in the browser and assist someone who is assembling an IT ticket over the phone or email.
/*
Alias function to shorten `document.getElementById` calls
*/
const getElemById = function(elemId) {
return document.getElementById(elemId);
};
/*
Alias function to shorten `addEventListener("change", refreshCallerValues)` calls
*/
const addRefreshListener = function(elemId) {
getElemById(elemId).addEventListener("change", refreshCallerValues);
};
refreshCallerValues
? Is that supposed to be a parameter toaddRefreshListerner
? Or is it a separate function declared elsewhere?