1

Until recently I've only known immediately executed functions to have this syntax:

(function(){
  …
})();

Then I was looking at a library and found:

! function(){
  …
}();

Are the two equivalent approaches? Meaning, do they have the same consequences from an execution perspective?

Mark Fox
  • 8,694
  • 9
  • 53
  • 75
  • this appears to no longer be a question. more of a speech/blog post. Do you have any remaining questions? – Ben McCormick Mar 11 '13 at 01:04
  • I leave the question open since there may be answers which I missed. Since I doubt the scope of my own knowledge I'm not in the position to make that call. I could simply move my research into an answer if you think it would clarify something. – Mark Fox Mar 11 '13 at 01:06
  • I flagged this as not a real question, because questions here are supposed to be specific and answerable. This reads more as "here's a topic I'm interested in, lets talk about it." Nothing wrong with that in general, but its not how SO works. – Ben McCormick Mar 11 '13 at 01:09
  • I've moved the "update" to an answer to fit the Q&A format as the question remains distinct from any other S.O. question. – Mark Fox Mar 11 '13 at 01:25

1 Answers1

1

This blog post has a really thorough discussion of the general concept.

Upon starting this question the term 'self executing' was what I knew as I've seen it in other contexts but during my research I discovered the term 'immediately invoked' which is more accurate since the execution occurs outside the function body. I've left 'self' in the question to aid others.

If you're curious about the why preceding with an operator works check this answer to 'Javascript Self Executing supposed to work?' which elaborates as well as this answer to 'Explain JavaScript's encapsulated anonymous function syntax' which also has useful information about encapsulating functions for immediate execution.

From a performance perspective this blog post 'Self-Invoking Functions' compares between !,-,+,~,void,delete operators and parens. The differences appear to be negligible so readability is probably preferred: as the authors puts it it is still recommended to use the parentheses or a bang [!] because they are programming conventions.

One obvious effect of preceding with an operator is the way each operator will effect the return type, but if you are capturing the return value via assignment (which seems to be the only way with a immediate invocation) then you can omit an operator like ! since the assignment operator allows the parser to immediately execute the function: var capture = function(){ return 'capture this' }();

Another edge case is that you can use the new keyword with parentheses but not when preceding with an operator — see this question 'Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?' which has an interesting answer revealing how the placement of the executing parentheses and the use of new can change the behavior of the encapsulation, which (as stated) is not possible when preceding with an operator.

Community
  • 1
  • 1
Mark Fox
  • 8,694
  • 9
  • 53
  • 75