2

i see the following code in a lot of java-script files.

(function () {
    //code goes here
})();

Can somebody explain this, or point me to a tutorial that explains this?

voilbak
  • 39
  • 4

1 Answers1

1

This is a code block, an immediately executing function to be exact, which basically prevents variables you define to leak into a global scope (which is a horrible thing and can lead to hard to track bugs)...

There is a good video you can watch on youtube Fundamentals for Great jQuery Development where a much more knowledgeable person goes over this technique in the first part of it. I advise you to watch it :)

EDIT
Fixed the link to point to an intended video

Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43