Convert string to function without using eval.

Avatar

Avatar

tarekahf

Avatar

tarekahf

tarekahf

20-08-2018

Please check this article:

https://www.sitepoint.com/call-javascript-function-string-without-using-eval/

I am looking for a way to convert string to a function call without using eval. The reason for this is that I am using Json to store Appraisal Valuation Rules, and sometimes the rule is so complex that it has to be in a dedicated function. So, I decided to use standard function with two parameters only. The rule will look something like the following:

    {

     clients: ["Client 1", "Client 4", "Client 3"],

     riskKey: "Risk_Key_XYZ",

     riskCriteriaTempl: "('{0}' === 'Full' && '{1}' === 'No')",

     riskRuleFields: ["FORM_TYPE", "other_land_conforms"],

     highlightFields: [1],

     riskProcessFunc: "xyz_function()",

     riskFuncFields: {field1:"field1", field2:"field2"},

     riskLevel: "High",

     riskName_en: "Risk Name English",

     riskName_fr: "Risk Name French",

   }

So basically, the property "riskProcessFunc" will have the function name, and instead of using this format:

{...

riskProcessFunc: function (riskFields) {return xyz_function(this, riskFields)}

}

I will use this simplified format which can then be easily stored in Database or Excel Sheet:

{...

riskProcessFunc: "xyz_function()"

}

To make it clear, use the sample below:

function complexRule_xyz(a, b) {

    console.println("It is working: " + a + ", " + b);

    return "Yes!";

}

var funcName = "complexRule_xyz";

var myFn1 = new Function("p1", "p2", "return p1 + ', ' + p2");

var myFn2 = new Function("p1", "p2", "return " + funcName + "(p1, p2)");

var myFn3 = eval(funcName);

myFn1("po", "we");

myFn2("er", "rt")

myFn3("tr", "yu");

Try to execute the above using Console (ctrl-j) in Adobe Acrobat, and you will see that "myFn2()" won't work. Best is to use "eval()".

So ref. to the above article, the bottom line is how to use the "window" object from Adobe LiveCycle Designer Javascript?

Do you have other suggestions?

Tarek

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Magus069

MVP

Avatar

Magus069

MVP

Magus069
MVP

20-08-2018

Hi there,

I'm not sure if this will do the job for you... but what if you store those functions within a JSON object with their respective names, and then call them using the JSON Object...

See the example below...

And then you can simply call the function using a string variable like the following :

I hope this will help you.

Answers (1)

Answers (1)

Avatar

Avatar

tarekahf

Avatar

tarekahf

tarekahf

20-08-2018

Thanks a lot Magus069​ ... sure this will work ...

I will try to implement this in the coming sprints. I only have 3-4 functions like that.

But I am always wondering, if our team is writing the code, then why we should be concerned about using "eval()"??! What risks it will have? No external party has access to change the possible value of the code to be executed by "eval()".

Tarek