I’m making something like a framework for JS. I want the users to be able to add functions to a specific action, just like in wordpress plugins:
add_action("wp_head", "functionName");
I looked at other questions, and the most appreciated answer was
window['functionName']
But how do I do it when the function “functionName” is not global, but sits within $(document).ready({}); for example? It throws an error Uncaught TypeError: Cannot call method ‘call’ of undefined
There is one more option, that is to pass the user’s function as an argument to add_action (if we suppose that my function will be named add_action as well):
add_action("some_event", functionName);
// in the framework's JS file:
function add_action(event, fn) {
fn();
}
But I have a hunch that this will be inefficient as hell and a wrong way to do it.
The last way — passing in the function — is actually the best way to do it. It is neither inefficient nor wrong.