Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why/Where/When should I do this? (enclose js code inside a function and then execute it) [duplicate]

Possible Duplicate:
JavaScript scope and closure

What is this for?

(function(){
     //The code to be executed
})(); 

Also, has this anything to do with closures?

like image 208
DanC Avatar asked Aug 31 '10 18:08

DanC


1 Answers

I've seen it often recently. I'm guessing:

No need to name the function. It implies it's not reusable. This gives you a local scope to declare variables using var (otherwise, you would add them to global).

like image 53
gawi Avatar answered Sep 20 '22 20:09

gawi