Listing functions by their function name

I have several functions that are used to bind events with a div on the page. There MenuHandlers, CalendarHandlers, PopupHandlers ....

So, at the moment I have one function, called BindHandlers, which I call from the document ready function, and which runs all the handler functions one by one.

Is there a one-line solution that says something like “call all functions that have“ handlers ”in their function name?

This is what the function looks like:

function BindHandlers() { MenuHandlers(); CalendarHandlers(); PopupHandlers(); ... 8 more like this } 

What I want to replace:

 BindHandlers(); // replace that line with the following: "call all the functions that have "Handlers" in their names 

Thanks for the suggestions.

+4
source share
2 answers

If these functions are global, you can call all functions with Handlers in the name using something like this

 for (var x in window) if (typeof window[x] === 'function' && /Handlers/.test(x)) window[x](); 

Here is a simple demo

+4
source

The trunk has the following:

 events: { 'click #menu' : 'MenuHandler', 'click #calendar' : 'CalendarHandler', // etc. } 

Event binding is automatic when the corresponding view is created.

You can use this template:

 bindEvents({ 'click #menu' : 'MenuHandler', 'click #calendar' : 'CalendarHandler', // etc. }); 
0
source

Source: https://habr.com/ru/post/1397128/


All Articles