0

I would like to store a function definition in a string, like for example:

var funcString = 'function(){ return 4+4 }'

Can I use eval() to evaluate the function?

Like for example:

var result = eval(funcString) 

which will evaluate the string in funcString as a function and return 4+4 (8) in result.

edit: So from what you've told me I understood that I shouldn't use eval in that way, but for my case I don't think there is another way.

I would like to define a set of rules in a separate file and I want to instruct my Javascript Library to look into this file and search for the rules. The following is an example of a rule:

rule('logOut', (function() { return !JSLibrary.compareUserDetails })(), (function() { console.log("\n\nError: logged out user did not match\n\n\n") })())

I would like to evaluate both of the functions defined in the rules and I think that eval is the only way to do it. I don't know if there exist any other.

Bernard Mizzi
  • 49
  • 1
  • 1
  • 8
  • 2
    Think very, very carefully about *why* you want to do this, as it is a terrible idea by just about every conceivable metric. Also, `let eight = new Function('return 4 + 4;')();`. Also terrible. – Jared Smith Mar 13 '18 at 17:22
  • 1
    The function is added to the top-level scope, and you didn't give it a name. –  Mar 13 '18 at 17:22
  • Don't use eval for anything ever it's a horrible practice. Sure there might be a few rare scenarios it is deemed acceptable but I don't think they apply. – Nope Mar 13 '18 at 17:23
  • Possible duplicate of [How can I use js eval to return a value?](https://stackoverflow.com/questions/7399024/how-can-i-use-js-eval-to-return-a-value) – Nope Mar 13 '18 at 17:26
  • Yes, I'm sorry that I didn't notice it before. I'll edit my question to provide what I'm really looking for. – Bernard Mizzi Mar 13 '18 at 17:35
  • @Nope, question is now edited it. Thanks for pointing it out. – Bernard Mizzi Mar 13 '18 at 17:43

2 Answers2

2

Fair warning, eval is "evil." I'll answer your question regardless, but you might want to rethink using eval.

In JavaScript, functions are values, just like 5, 'Stack Overflow' and document. In fact, what function myFunction(args) { body } does is create a function and throw it into a variable called myFunction*.

Say you wanted to take that function and put it into another variable. You can just say var otherVariable = myFunction. And if you want to call that function, you can just say otherVariable().

But say you want a function that isn't bound to a name, for instance if you're passing it to another function. You can use an anonymous function, defined as function(args) { body }. That's what's inside the string you're eval-ing. Anonymous functions are just like any other function, and you can call them as such. Actually, you can even call them right out of the result of eval by tacking on some parentheses at the end.

Unfortunately, it's not that easy though. You can't just write a line of code with an anonymous function definition and expect it to run: you will instead get an error complaining about it not being named. To fix this, throw some parenthesis around the definition to force it to be an expression instead of a statement.

Your final working code will look like this:

var funcString = '(function(){ return 4+4 })'
var result = eval(funcString)()

Sure, you should never use eval for this particular use, but I hope you learned a bit about JavaScript from this answer anyways.

* There's more to it than that, such as hoisting, but that doesn't really matter in this explanation.

Piper McCorkle
  • 1,044
  • 13
  • 27
  • Did you test this? `var funcString = 'function(){ return 4+4 }'` won't compile in Chrome. –  Mar 13 '18 at 17:33
0

Use of an immediately invoked function expression can call a function with eval.

document.body.innerHTML = eval('(function(){return 4+4;})()');

WARNING: do not ever do this: it's insecure, a performance-killer, harder to read/understand, etc

Community
  • 1
  • 1
Namaskar
  • 2,114
  • 1
  • 15
  • 29
  • While correct, this answer could be improved by warnings to not ever do this: it's insecure, a performance-killer, harder to read/understand, etc. – Jared Smith Mar 13 '18 at 17:24