I learned I can call functions by a string representing the function name. Example here: http://www.sitepoint.com/call-javascript-function-string-without-using-eval/
My question is, how can I call the SubTest
function in this case:
function Test() {
this.SubTest = function() { }
}
var functionString = 'Test.SubTest';
It doesn't work with window[functionString]
.
I even tried this (it's basic; just for testing) but it returns false
:
function GetFunction(functionName) {
var functions = functionName.split('.');
if ( functions.length === 1 ) {
var functionObj = window[functions[0]];
if ( typeof functionObj === 'function' )
return functionObj;
return false;
}
else if ( functions.length > 1 ) {
var functionObj = window[functions[0]];
for ( var i = 1; i < functions.length; i++ ) {
functionObj = functionObj[functions[i]];
}
if ( typeof functionObj === 'function' )
return functionObj;
return false;
}
return false;
}
var functionName = 'Test.SubTest'; // from the above code example
var functionObj = GetFunction(functionName); // returns false
Update:
Found this: How to turn a String into a javascript function call? but the getFunctionFromString
still doesn't work.