0

Can someone explain why these 2 functions behave differently.

Snippet 1:

function operate1(operator) {
 return function(x, y) {
 return x + operator + y;
 }
}

Snippet 2:

function operate2(operator) {
 return new Function("x", "y", "return x " + operator + " y;");
}

Usage:

adder1 = operate1("+");
adder2 = operate2("+");
adder1(5, 3); // returns "5+3"
adder2(5, 3); // returns 8

I am particularly curious of why operate2 evaluated the arithmetic expression when I thought it would evaluate it as a string at first glance. Does this have something to do with it being defined as a Function Object with the new operator?

asked Jul 14, 2015 at 9:30
1
  • Because return x + operator + y; is concatenating the operator Commented Jul 14, 2015 at 9:32

2 Answers 2

3

The first does string concatenation due to the operator being a string

return x + "+" + y;

The second performs an evaluation of the content because that is how new Function works - the result is similar to eval but have a look at the differences here: Are eval() and new Function() the same thing?

So the statement

new Function("x", "y", "return x " + operator + " y;");

has the "return x " + operator + " y;" part evaluated

Here is the second version behaving like the first

function operate2(operator) {
 return new Function("x", "y", "return x +'" + operator + "'+ y;");
}
var adder2 = operate2("+");
alert(adder2(5, 3))

answered Jul 14, 2015 at 9:32
Sign up to request clarification or add additional context in comments.

5 Comments

Isn't it the same for the second one as well? It should have evaluated as "return x " + "+" + " y;" where "+" is a string too.
No, new Function() has the almost the same functionality as eval under the hood
is there a documentation that supports your statement that it performs eval on the last argument?
Have a look at the link I gave. It is not performing an eval, but the string is evaluated
I updated the answer to include the string version in adder2
1

It's following the exact description in this documentation on the Mozilla site: Function - JavaScript

The constructor signature:

new Function ([arg1[, arg2[, ...argN]],] functionBody)

functionBody is to be a string containing the JavaScript statements comprising the function definition.


What if you actually wanted adder2 to return a string? This would do it:here's some code that constructs code that generates the expression.

function operate2(operator) {
 return new Function("x", "y", "return '' + x + '" + operator + "' + y;");
}
answered Jul 14, 2015 at 9:37

1 Comment

@krato - Please try again - I've gotten it to work: jsfiddle.net/2g9axxhs

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.