I want to dynamically call a function from a string like "User.find". A script would call the function find() in the object User if the function exists. Here's what I tried:
var User = {};
User.find = function(){
return 1;
}
var input = 'User.find';
var some_data_array = {name: 'John Doe'};
var method = input.toString().split('.');
var nameObj = method[0].substring(0,1).toUpperCase() + method[0].substring(1);
var methodToCall = method[1];
nameObj.call(methodToCall, some_data_array);
But it always returns:
nameObj.call(methodToCall, some_data_array);
TypeError: Object User has no method 'call'
Any idea? I can't use window since it is a node.js problem, the script is not executed in the browser.