So far I've learned the benefits of using this function (is it wrapping?)
So, it almost acts like namespaces. Suppose we have:
( function() { function foo(){ alert(true); } foo(); //alerts true })(); ( function() { function foo(){ //the same title of the function as above alert("another call of foo"); } foo(); //alerts, ok. })(); Also I've noticed it can access public vars, like this:
var __foo__ = 'bar'; ( function() { alert(__foo__); //alerts bar })(); I have several questions regarding this approach
What I've tried:
- Use Bing for tutorials (I' found them, but many of them don't answer my questions)
- Play with passing objects into the body
- Find the answer here
But, I'm still beating my head against the wall
So the questions are:
I've seen people pass objects as params, but when DOES it make sense? For example, what does it mean?
( function(window) { })(document);I saw smth like this in Jquery UI Lib
( function($) { //some code of widget goes here })(Jquery);
This makes inner code visible outside the function, right? (not sure) Why, this is because we can access the object (say we have "modal" widget), simply by calling it,
like:
$(function(){ $("#some_div").modal(); //here it's object the we got from the function }); And the second question is: How does it work.
(function(){})(), as the title suggests? Or are you just asking how it works and when to use it?