PDA

View Full Version : self executing functions


Adam_C
03-15-2011, 09:19 PM
Hi,

I've recently come across the code

(function(){
//...Code here
})


What is the difference with wrapping your code with this, rather than just writing it in the script tags...?

DWcourse
03-16-2011, 01:14 AM
was this on a php page or perhaps inside a script?

Adam_C
03-16-2011, 02:23 PM
I saw it inside JS

DWcourse
03-16-2011, 03:13 PM
the script tags wrap the javascript code (or the link to the javascript file). This is actually defining a function that can then be used somewhere else.

ranjan
03-16-2011, 09:42 PM
the code would have been


(function(){
//...Code here
})();


These are called self executing functions or self invoking functions

http://briancrescimanno.com/2009/09/24/how-self-executing-anonymous-functions-work/