Using React-native on android. However, I think my question applies to any javascript environment.
I am constructing a function from text sent from the server (there are good reasons for it).
function helper_called_from_dynamic (arg1)
{
console.log('helper called ',arg1);
}
export class MyInvoker
{
constructor ()
{
this._funcProps={};
}
initialize ( item )
{
this._funcProps["df1"]=new Function (["inArg1"],item.fnBody);
}
call_dynamic_func (fnName,arg1)
{
return this._funcProps[fnName](arg1);
}
}
The fnBody has the following: " return helper_called_from_dynamic(inArg1); "
my invocation via MyInvoker is the following
let invInst = new MyInvoker();
let item={fnBody:"return helper_called_from_dynamic(inArg1); "};
invInst.initialize(item);
invInst.call_dynamic_func("df1","somearg");
I am getting an error (from react-native, but again, I suspect it would be common to all other javascript environments):
cannot find variable: helper_called_from_dynamic
Is it possible to get this to work? That is allowing the dynamically created functions to call other functions? Or do I have to resort to 'eval' ?