How to create a large function made of functions in an array, concatenated with logical AND?

Just like in my other question, I am looking for a way to have a button return a series of functions concatenated with a logical AND (&&). My new approach uses eventListeners, much like in the comment described at the previous question, and with the function getEventListeners I can generate a list of functions present on the button.
The reason that I want to generate this, instead of just typing it out, is that I want to write future-proof code, and therefore to have modular functions instead of hard-coding everything. I also have the feeling this should be possible and I am just bad at wording my search queries.

To concatenate those with the logical AND, according to this answer one can use a map reduce. However, this would require me passing the list of functions to the button when clicking, instead of my preferred solution of having one function containing all the other functions, concatenated. For visualisation, what I want to achieve is:
button.addEventListener("click", function() { return function1() && function2() && etc(); });

Earlier, I tried to do this without a list, trying to accumulate the function in a loop:

static addCumulativeEvent(object, eventType, newFunc) {
   let eventsOnObject = object.getEventListeners(eventType);
   let cumulativeFunction = newFunc;
   for (let eL of eventsOnObject) {
      cumulativeFunction = function() { return cumulativeFunction() && eL.listener(); };
   }
   <add cumulativeFunction to object>
}

However, this gave the error of "too much recursion", and I found no good way of avoiding this recursion.

So the question boils down to: is there a straightforward way to create a function made up of multiple functions in a list, or is there another way to concatenate all of them using a loop?

1 thought on “How to create a large function made of functions in an array, concatenated with logical AND?”

Leave a Comment