Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reasoning behind nameless functions

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
This is becoming more common in a lot of code on the internet: something called immediately invoked function expression
Code:
// some code
(function(){
   //do some work 
})();
// more code
The bit I don't understand it why people do that instead of just
Code:
// some code
// do some work
// more code
It is actually 2 lines less. Also why do so many coders use inline variable assignments to function expressions. Why not just declare the function first and then call it like in any other language. Instead of
Code:
// function decl
// some code
x = decl(...)
// more code
we get
Code:
// some code
x = (function (...) {
   // function body
   } (...)
// more code
It is actually more difficult to read because you cannot see the flow so easily as it is obscured by the function body (which can be pretty big).

What is the reasoning behind IIFE other than making code difficult to read, decipher and maintain?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top