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?

Randomblue
  • 112,777
  • 145
  • 353
  • 547

4 Answers4

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.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • _"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 May 31 '12 at 07:45
  • @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. – Corbin May 31 '12 at 07:45
  • 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 May 31 '12 at 07:50
  • @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. :) – Corbin May 31 '12 at 08:14
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.

Confusion
  • 16,256
  • 8
  • 46
  • 71
0

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

var func = function(){};
new Function(func)
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
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>
Tuhin Paul
  • 199
  • 2
  • 10