Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(...)?

like image 601
user3470598 Avatar asked Jul 06 '15 23:07

user3470598


1 Answers

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.

like image 155
Korikulum Avatar answered Nov 15 '22 08:11

Korikulum