A widget typically contains a slightly complex mixture of HTML, CSS and Java Script.
You will want to hide that complexity and make it as easy as possible for people to include your widget on their pages.
Because you can’t predict what Java Script code will be running on the page which uses our widget, we need a way to ensure that it doesn’t clash with any other Java Script code included on the host page.
To do that, we just enclose all our code within an anonymous function and we call that function.
I created some web widgets for the London’s Design Museum and learned a few useful things in the process.