Two reasons:
contamination
The first is for preventing global contamination for any of the variables needed for the bookmarklet. Using a closure means that var a
wont add window.a
to the global context. Additionally, using a named function would add it to the global namespace. function a() {...}
would add window.a
.
accidental DOM recreation
The second is to avoid accidentally recreating the DOM by returning a string. Any javascript:
url where a string is returned will create a brand new DOM using the contents of the string as the source.