What is the difference between new Function and Eval?

Asked

Viewed 528 times

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

2 answers

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.

  • I think you were wrong about the scope of variables, can you give an example where the Function does not access the variables eval can?

  • The big difference is the same scope.

  • @bfavaretto the big difference is that Function returns a function, and eval cannot return a function, returns the result of an expression.

  • Yes @Kaduamaral, but Function is much more useful (and more used) to create a eval than to create function instances to be used later.

5


val

The Eval() method evaluates Javascript code represented as a string. MDN

Function

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.

Examples:

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

  • 1

    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

You are not signed in. Login or sign up in order to post.