0

JavaScript has a Function constructor which produces an anonymous function:

new Function()

When passing a function as an argument to Function, I get a syntax error:

new Function(function(){})

produces

SyntaxError: Unexpected token (

However, when I pass a number, all is fine:

new Function(10)

Why do I get a syntax error when passing a function to Function?

2
  • 4
    Because then the world would explode :). Commented May 31, 2012 at 7:09
  • you can try to do it like this: stackoverflow.com/questions/2274695/… Commented May 31, 2012 at 7:11

4 Answers 4

2

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function

Considering all but the last arguments are argument names, I wouldn't expect anything other than a string to work.

I would imagine the syntax error is because of the way the JS engine you're using internally tries to convert the function to a string. I'm actually surprised it doesn't choke on the 10.

Also, I suspect you're doing this just out of curiosity, but if you're not, I suggest you not use Function in code you can control. There's not really a reason to use Function unless you need to take a string and make a function out of it at run time.

Sign up to request clarification or add additional context in comments.

4 Comments

"I'm actually surprised it doesn't choke on the 10." I guess as with a function JS converts 10 to a string and then tries to use that as the new function's body, except that a string containing just a number actually is a valid function body. 10 all by itself is just an expression that doesn't do anything, but it isn't actually a syntax error. 10 \n 11 \n alert(12) will work (even without semicolons as long as there are actual newlines where indicated; 10;11;alert(12); works all on one line).
@nnnnnn Of course it will, but Function is a different context. 10 as a variable name is invalid, so I would expect it to throw an exception or error in some way. I would not expect a syntax error, though I think one in the case of passing a function is completely understandable since passing a function to it can be caught at parse time. (So really, with a literal new Function(10), I would understand a syntax error too. var a = 10; new Function(a); I would not expect one. By the syntax of the language it's valid, I just figure the parser may as well catch it.
Sorry, I was editing my comment at the same time you were replying to it. The parameter to new Function() needs to be a string - or, apparently, something that can be converted to a string - that itself is valid as a function body. When a function is converted to a string it includes the word "function" and the parameters, etc.: "function() {}", so that doesn't make a valid function body. "10" is a valid function body.
@nnnnnn Ah... Didn't think about it from that perspective. For some reason I wasn't realizing that there must be 0 or more argument names, and thus if only parameter is provided it becomes the function body. :)
2

Because the Function constructor expects String arguments and evaluates the last one to be the function body, expecting it to be valid javascript. When it attempts to evaluate the anonymous function argument as a String, it fails, because the String representation of that anonymous function is not valid javascript.

Comments

0

I don't know way that error, but you can try something like this:

var func = function(){};
new Function(func)

1 Comment

That'll have the same problem.
0

I would suggest using:

new Function(func)

instead of

new Function(func(){})

where you have to define func as a separate function. The following example demonstrates:

<html>
<script>
function init(func)
{
    func();
}
function greet()
{
    alert("good morning!");
}
</script>
<body onLoad="init(greet);">
<body>
</html>

Comments

Your Answer

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.