<p>There are two popular closure styles in javascript. The first I call <em>anonymous constructor</em>:</p>

<pre><code>new function() { 
  var code...
}
</code></pre>

<p>and the <em>inline executed function</em>:</p>

<pre><code>(function() {
  var code...
})();
</code></pre>

<p>are there differences in behaviour between those two? Is one "better" over the other?</p>

[{'answer_id': 6387, 'author': 'doekman', 'author_id': 56, 'author_profile': 'https://Stackoverflow.com/users/56', 'pm_score': 3, 'selected': False, 'text': "<p>@Lance: the first one is also executing. Compare it with a named constructor:</p>\n\n<pre><code>function Blah() {\n    alert('blah');\n}\nnew Bla();\n</code></pre>\n\n<p>this is actually also executing code. The same goes for the anonymous constructor...</p>\n\n<p>But that was not the question ;-)</p>\n"}, {'answer_id': 6424, 'author': 'olliej', 'author_id': 784, 'author_profile': 'https://Stackoverflow.com/users/784', 'pm_score': 5, 'selected': True, 'text': '<p>Both cases will execute the function, the only real difference is what the return value of the expression may be, and what the value of "this" will be inside the function.</p>\n\n<p>Basically behaviour of</p>\n\n<pre><code>new expression\n</code></pre>\n\n<p>Is effectively equivalent to</p>\n\n<pre><code>var tempObject = {};\nvar result = expression.call(tempObject);\nif (result is not an object)\n    result = tempObject;\n</code></pre>\n\n<p>Although of course tempObject and result are transient values you can never see (they\'re implementation details in the interpreter), and there is no JS mechanism to do the "is not an object" check.</p>\n\n<p>Broadly speaking the "new function() { .. }" method will be slower due to the need to create the this object for the constructor.</p>\n\n<p>That said this should be not be a real difference as object allocation is not slow, and you shouldn\'t be using such code in hot code (due to the cost of creating the function object and associated closure).</p>\n\n<p>Edit: one thing i realised that i missed from this is that the <code>tempObject</code> will get <code>expression</code>s prototype, eg. (before the <code>expression.call</code>) <code>tempObject.__proto__ = expression.prototype</code></p>\n'}, {'answer_id': 6436, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 0, 'selected': False, 'text': '<p>Well, I made a page like this:</p>\n\n<pre><code>&lt;html&gt;\n&lt;body&gt;\n&lt;script type="text/javascript"&gt;\nvar a = new function() { \n  alert("method 1");\n\n  return "test";\n};\n\nvar b = (function() {\n  alert("method 2");\n\n  return "test";\n})();\n\nalert(a);  //a is a function\nalert(b);  //b is a string containing "test"\n\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>Surprisingly enough (to me anyway) it alerted both "method 1" and method 2".  I didn\'t expect "method 1" to be alerted.  The difference was what the values of a and b were.  a was the function itself, while b was the string that the function returned.</p>\n'}, {'answer_id': 6489, 'author': 'Adhip Gupta', 'author_id': 384, 'author_profile': 'https://Stackoverflow.com/users/384', 'pm_score': -1, 'selected': False, 'text': '<p>Yes, there are differences between the two.</p>\n\n<p>Both are anonymous functions and execute in the exact same way. But, the difference between the two is that in the second case scope of the variables is restricted to the anonymous function itself. There is no chance of accidentally adding variables to the global scope.</p>\n\n<p>This implies that by using the second method, you are not cluttering up the global variables scope which is good as these global variable values can interfere with some other global variables that you may use in some other library or are being used in a third party library.</p>\n\n<p>Example:</p>\n\n<pre><code>&lt;html&gt;\n&lt;body&gt;\n&lt;script type="text/javascript"&gt;\n\nnew function() { \na = "Hello";\nalert(a + " Inside Function");\n};\n\nalert(a + " Outside Function");\n\n(function() { \nvar b = "World";\nalert(b + " Inside Function");\n})();\n\nalert(b + " Outside Function");\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>In the above code the output is something like:</p>\n\n<blockquote>\n  <p>Hello Inside Function<br>\n  Hello Outside Function<br>\n  World Inside Function</p>\n</blockquote>\n\n<p>... then, you get an error as \'b\' is not defined outside the function!</p>\n\n<p>Thus, I believe that the second method is better... safer!</p>\n'}, {'answer_id': 6542, 'author': 'Kieron', 'author_id': 588, 'author_profile': 'https://Stackoverflow.com/users/588', 'pm_score': 2, 'selected': False, 'text': "<p>They both create a closure by executing the code block. As a matter of style I much prefer the second for a couple of reasons:</p>\n\n<p>It's not immediately obvious by glancing at the first that the code will actually be executed; the line <em>looks like</em> it is <em>creating</em> a new function, rather than executing it as a constructor, but that's not what's actually happening. Avoid code that doesn't do what it looks like it's doing!</p>\n\n<p>Also the <code>(function(){</code> ... <code>})();</code> make nice bookend tokens so that you can immediately see that you're entering and leaving a closure scope. This is good because it alerts the programmer reading it to the scope change, and is especially useful if you're doing some postprocessing of the file, eg for minification.</p>\n"}]
['https://Stackoverflow.com/questions/6373', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/56/']