8
What’s the difference between new Function
and eval
, since the two seem to be doing the same thing?
Example with eval
:
eval('1 + 1'); // 2
Example with new Function
:
new Function('', 'return (1+1)')() // 2
8
What’s the difference between new Function
and eval
, since the two seem to be doing the same thing?
Example with eval
:
eval('1 + 1'); // 2
Example with new Function
:
new Function('', 'return (1+1)')() // 2
6
They don’t do the same thing:
eval()
evaluates a string as a Javascript expression within the current scope and can access local variables.
new Function
parses the code for a function object, which can be called. It cannot access local variables because the code runs in a separate scope.
What does that mean? Run this code:
function testEval() {
var a = 'original';
eval("(a = 'alterado')");
console.log(a);
}
function testNewFunction() {
var a = 'original';
new Function("(a = 'alterado')")();
console.log(a);
}
testEval()
testNewFunction()
When we use eval
the value of the variable a
is changed. When we use new Function
it remains unchanged.
5
The Eval() method evaluates Javascript code represented as a string. MDN
The Function constructor creates a new Function Object. In Javascript Every Function is Actually a Function Object. - MDN
Free translation:
The builder
Function
creates a new anonymized function object. In Javascript all functions are currently a function object.
The documentation explains itself, basically the val executes an expression, statement etc. Already the builder Function creates a new function object entitled to its entire prototype.
var a = new Function('', 'return (1+1)');
var b = eval("2 + a()");
var c = b + a();
var d = new Function("p", "return a() + b + c + p");
var e = eval("d(10) - 20");
console.log(a()) // 2
console.log(b) // 4
console.log(c) // 6
console.log(d(10)) // 22
console.log(e); // 2
On the website of W3schools has this example in the constructor session Function
:
var myFunction = new Function("a", "b", "return a * b");
var x = myFunction(4, 3);
And right after you say the following followed by another example:
You Actually don’t have to use the Function constructor. The example above is the same as writing:
var myFunction = function (a, b) {return a * b}; var x = myFunction(4, 3);
The sentence reads as follows (free translation):
Currently you do not need to use the function builder. The example below has the same effect.
The undescore.js
uses the new Function
:) in creating templates
Cool @Wallacemaxters, actually I think I’ve never seen the new Function
. I’m new with Javascript, not from my time... kkkkk
Browser other questions tagged javascript eval
You are not signed in. Login or sign up in order to post.
I think you were wrong about the scope of variables, can you give an example where the
Function
does not access the variableseval
can?– KaduAmaral
The big difference is the same scope.
– bfavaretto
@bfavaretto the big difference is that
Function
returns a function, andeval
cannot return a function, returns the result of an expression.– KaduAmaral
Yes @Kaduamaral, but
Function
is much more useful (and more used) to create aeval
than to create function instances to be used later.– bfavaretto