What's the difference between these functions created by `Function`?

102 views Asked by At

1.var f = new Function("a", "b", "return a+b")

2.var f2 = Function("a", "b", "return a+b")

f and f2 both are a anonymous function. f(1,2) and f2(1,2) both returns 3. So is there any actual internal difference between the two? Does Function internally return a function object? Difference from using the Function as constructornew Function(...)?

1

There are 1 answers

1
Korikulum On

From the ECMAScript 5.1 specs:

When Function is called as a function rather than as a constructor, it creates and initialises a new Function object. Thus the function call Function(…) is equivalent to the object creation expression new Function(…) with the same arguments.