Are these two functions doing the same thing behind the scenes? (in single statement functions)
var evaluate = function(string) {
return eval(\'(\' + str
In that example, the results are the same, yes. Both execute the expression you pass. This is what makes them so dangerous.
But they do different things behind the scense. The one involving new Function()
, behind-the-scenes, creates an anonymous function from the code you supply, which is executed when the function is invoked.
The JavaScript you pass to it is technically not executed until you invoke the anonymous function. This is in contrast to eval()
which executes the code right away, and doesn't generate a function based on it.