Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  195] [ 5]  / answers: 1 / hits: 23938  / 14 Years ago, mon, may 3, 2010, 12:00:00

I have a bit of JavaScript code that is specified in a configuration file on the server-side. Since I can't specify a JavaScript function in the configuration language (Lua), I have it as a string. The server returns the string in some JSON and I have the client interpret it using a clean-up function:



parse_fields = function(fields) {
for (var i = 0; i < fields.length; ++i) {
if (fields[i].sortType) {
sort_string = fields[i].sortType;
fields[i].sortType = eval(sort_string);
}
return fields;
}
};


So basically it just evaluates sortType if it exists. The problem is that Firebug is reporting a Syntax error on the eval() line. When I run the same steps on the Firebug console, it works with no problems and I can execute the function as I expect. I've tried some different variations: window.eval instead of plain eval, storing the sortType as I've done above, and trying small variations to the string.



A sample value of fields[i].sortType is function(value) { return Math.abs(value); }. Here's the testing I did in Firebug console:



>>> sort_string
function(value) { return Math.abs(value); }
>>> eval(sort_string)
function()
>>> eval(sort_string)(-1)
1


and the error itself in Firebug:



syntax error
[Break on this error] function(value) { return Math.abs(value); }


The last bit that may be relevant is that this is all wrapped in an Ext JS onReady() function, with an Ext.ns namespace change at the top. But I assumed the window.eval would call the global eval, regardless of any possible eval in more specific namespaces.



Any ideas are appreciated.


More From » function

 Answers
74

To do what you want, wrap your string in parentheses:



a = function(value) { return Math.abs(value);};
b = eval((+a+));
b(-1);

[#96895] Thursday, April 29, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;