6

Is there any difference in the two definitions and assignments of functions?

this.foo = new (function () {..})();

vs.

this.foo = function (){...};
Xerion
  • 3,141
  • 5
  • 30
  • 46
  • See also: http://stackoverflow.com/questions/2274695/new-function-with-lower-case-f-in-javascript and http://stackoverflow.com/questions/1895635/javascript-singleton-question – Christian C. Salvadó Aug 16 '10 at 16:43

3 Answers3

8

In the first example, it's creating a function and executing it, assigning the result to this.foo. In the second example, it's creating the function and assigning the function itself to this.foo.

Kiersten Arnold
  • 1,840
  • 1
  • 13
  • 17
  • this.foo in the second example, is equivalent to a function pointer correct? – Kenny Cason Aug 16 '10 at 16:25
  • @KennyCason: You can look at it that way. It is assigning the function to the variable `foo` in the current context. However, writing `function foo() {....}` will do *exactly* the same thing. – James Curran Aug 16 '10 at 16:29
  • 2
    @james not quite exactly, `function foo()` will asign it at the top of scope, `this.foo = function()` assigns it right there – Rixius Aug 16 '10 at 16:33
3

The main difference is calling the function with a "new" and assigning the anonymous function to a variable

ECMA 262 15.3.2: When Function is called as part of a new expression, it is a constructor: it initialises the newly created object.

So this.foo = new (function () {..})(); creates a new object and assigns it to this.foo

and

this.foo = function (){...}; defines an anonymous function and assigns it to this.foo

also another way is this.foo = (function () {..})(); (without the "new") will call the function and assign its return value to this.foo

In your case if you alert:

var foo = new (function () {})();
var bar = function() {};
var baz = (function () {return "boo";})(); // without the "new"

alert(foo); // will alert [object Object], the newly created object
alert(bar); // will alert function) {}, the function itself
alert(baz); // will alert "boo", the functions return value
naikus
  • 24,302
  • 4
  • 42
  • 43
-2

Your first example is plain wrong, have you actually tried it? If you want to use new and the Function constructor it works like this:

foo = new Function("function body");

(This is the worst possible way of defining a function)

Example 2 is the way to go.

ase
  • 13,231
  • 4
  • 34
  • 46
  • The anonymous function in the first example could return a function, which is then used with the `new` operator. Perhaps the anonymous function is some sort of object factory? – Ryan Kinal Aug 16 '10 at 16:39
  • Actually @Ryan it won't do that unless you wrap the right side of that expression (i.e., the whole function call) in another layer of parens. The "new" operator binds at least as tightly as (), so it's like `(new (function() { ... })) ()` as currently written. – Pointy Aug 16 '10 at 16:41
  • `new function() { ... }` is fine to do. It doesn't do the same thing as `new Function()`. It does the same thing as `new MyConstructor()`, which is perfectly valid. – Alex Wayne Aug 16 '10 at 16:53
  • @Pointy - you're right that I need another layer of parentheses, but I think it would be more like `foo = new (function() {...}())();` Either way, not a very easy syntax to use, and it defeats the purpose of an object factory anyway. – Ryan Kinal Aug 16 '10 at 18:04