Both of these code blocks below alert foo then bar. The only difference is })() and }()).
Code 1:
(function()
{
bar = 'bar';
alert('foo');
})();
alert(bar);
Code 2:
(function()
{
bar = 'bar';
alert('foo');
}());
alert(bar);
So is there any difference, apart from the syntax?
No; they are identical
However, if you add
newbeforehand and.somethingafterwards, they will be different.Code 1
This code creates a new instance of this function’s class, then gets the
propproperty of the new instance.It returns
4.It’s equivalent to
Code 2
This code calls
newon theClassproperty.Since the parentheses for the function call are inside the outer set of parentheses, they aren’t picked up by the
newexpression, and instead call the function normally, returning its return value.The
newexpression parses up to the.Classand instantiates that. (the parentheses afterneware optional)It’s equivalent to
Without the parentheses around the call to
getNamespace(), this would be parsed as(new getNamespace()).Class— it would call instantiate thegetNamespaceclass and return theClassproperty of the new instance.