I once read that it's good practice to enclose your whole script inside a self invoking function instead of just directly writing it to the document.
eg. Do this:
(function ()
{
//code here
})():
Although when I do this, it's more difficult to debug because all variables are outside of scope, and also I can't create objects from external classes. I remember the source saying that this should be done as a security measure. Is this good practice? If so, why?