views:

46

answers:

0

Possible Duplicates:
Why would one write global code inside a function definition-call pair?
How does the (function() {})() construct work and why do people use it?

I'm no JS ninja, and I'm now trying to learn the ins and outs of a language that has a lot more depth than I thought!

I've seen a lot of code recently (jquery among them) that's coded like this:

(function(){
var myLibrary = .....;
window.myLibrary = myLibrary;
})();

Can someone explain to me the benefit of this syntax? What's the difference between that and just writing something like:

var myLibrary = {
...
}