One important note is that in the code sample from Lasse the variable "result" 
will never see that it is being assigned a function.  It will take the type and 
value that the function returns, because the function executes before the 
assignment completes.

Thanks,
Austin Cheney, CISSP

From: [email protected] [mailto:[email protected]] On Behalf 
Of Lasse Reichstein
Sent: Wednesday, September 28, 2011 3:19 PM
To: [email protected]
Subject: Re: [JSMentors] self executing function


On Tue, Sep 27, 2011 at 9:31 PM, Xavier MONTILLET 
<[email protected]<mailto:[email protected]>> wrote:
(function(){
   //code
})();

is the module pattern and is widely used.
If you assign the result to a variable, there is no problem:

var result = (function(){
   //code
})();

In that case, you don't even need the wrapping parentheses:
 var result = function(){ ... }();
works too. People add them anyway, because it makes it easier to recognize the 
construct as an immediately called function literal.


About the !, it must be kind of the same idea but I don't know exactly
the difference...

It's the same idea: The parentheses in
 (function(){...})()
are needed because a statement can't start with the keyword "function" (then it 
has to be a function declaration, which is not what we want here).
Using '!' instead:
  !function(){ ... }()
is just one character shorter (although I'd argue it's less readable, but 
that's probably just habit).
/L
--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to