JSFiddle - React, Tailwind, and code Playground
JavaScript
window.namespace = { fun1: { fun2: { fun3: function() { alert('hi!') }}}};
runFunc = function(address) {
var addressArray = address.split('.'),
current = window,
i = 0;
for (i = 0; i < addressArray.length; i++) {
current = current[addressArray[i]];
if (typeof current === 'undefined') {
throw('no value found at: '+ address);
}
}
if (typeof current === 'function') {
current();
} else {
throw('value is not a function at: '+ address);
}
};
runFunc('namespace.fun1.fun2.fun3');
try { runFunc('namespace.fun1.fun2.fun3.fun4'); } catch(e) { console.log('ERROR:'+ e) }
try { runFunc('namespace.fun1.fun2'); } catch(e) { console.log('ERROR:'+ e) }