0

I want to display a list of items, now sometimes these items' title will just be a plain string, and sometimes it might be a value returned by a function.

How can I make both events work using eval() ?

Here is an example code:

var a1 = "formatDate('" + startTime + "') + ' - ' + formatDate('" + endTime + "')"
var a2 = "#america"

var result1 = eval(a1) // works well!
var result2 = eval(a2) // doesn't work, need to use eval('a2') but then first one doesn't work

Only thing I can think of is when creating the string for example "#america" have it saved like "'#america'" instead, but I would rather avoid it

[edit]

Eventually I will have something like this:

arr.push("formatDate('" + startTime + "') + ' - ' + formatDate('" + endTime + "')");
arr.push("#america");
for(var i = 0; i < arr.length; i++) {
    var ev = eval(arr[i]);
    console.log(ev);
}
9
  • Why eval, you can directly call the function Commented Oct 7, 2015 at 10:52
  • 1
    "#america' should be "#america"; Commented Oct 7, 2015 at 10:53
  • Another thing, if your a2s gonna be a string, why do you need to eval() it? Commented Oct 7, 2015 at 10:54
  • @AdityaParab correct, thanks for the edit Commented Oct 7, 2015 at 10:55
  • I will ultimately have an array of values, I want to iterate on them and get their value, while the value might be just a plain string, or an expression I need to evaluate first. I don't know when it might be a string and when an expression Commented Oct 7, 2015 at 10:56

3 Answers 3

1

What I would suggest is wrapping the eval in a try catch block, if the eval succeeds then return the value otherwise return the value originally passed to function. There are many cases where eval can fail as it is simply trying to parse the string as valid JavaScript so any invalid JS not just a simple string can cause it to fail so its better to be safe and catch any error that comes out of it.

var evaluate = function(value) {
    try {
        return eval(value);
    }
    catch(err)
    {
        return value;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't solve what he's asking. Post this in a comment.
How doesn't it solve what he is asking? all he does with the evaluated code is log it to the console which in this case will work perfectly
I'll have to side with gabriel here; it's not pretty, but it does address the issue at hand. Depending on which circumstances OP has control over, it may well be the only way to solve the issue.
Right, I get what you mean now, gabriel. I've removed the downvote, but I certainly don't think wrapping it in a try catch statement is the way to go.
Ideally he wouldn't be using eval at all but as David said if he doesn't have control over the input there really is no other way.
|
1

var ev = eval(a2) would be equivalent to var ev = eval('#america') which doesn't make any real sense.

When you say eval('a2') works, I assume that ev = '#america' is the desired outcome. The 'a2' expression is evaluated as simply accessing the value of the variable of that name.

You're basically just having a series of strings that may be valid javascript code, or may not, and there's no way to tell which is which. In that case, the best you can do is something like

try { 
  ev = eval(arr[i]);
} catch(ex) {
  ev = arr[i];
}

... which obviously looks terrible. Can you control the content of the entries in arr?

arr.push(function() { 
   return formatDate(startTime) - formatDate(endTime);
});
arr.push("#america");

In that case, you could check for the type of each entry, and act on it accordingly:

for(var i = 0; i < arr.length; i++) {
  var ev = typeof arr[i] == 'function' ? arr[i]() : arr[i];
  console.log(ev);
}

Comments

0

this is that i should do:

var a1 = function(){ 
            return formatDate(startTime) + formatDate(endTime) 
         }
var a2 = "#america"
var result1 = a1();
var result2 = a2;

Yo can check with typeof(a1) if the var is a function or an object or anyting else.

if(typeof(a1)=='function'){
   result = a1();
}else{
   result=a1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.