Link to home
Start Free TrialLog in
Avatar of cklanac
cklanac

asked on

parentheses around javascript function

The YUI framework from Yahoo has a simple demo here (http://developer.yahoo.com/yui/examples/dom/setxy_clean.html) that uses an odd technique which wraps a function in parentheses. I haven't seen this technique before and can't find any references to it online. What is it supposed to accomplish?
Why this?
<SCRIPT>
(function() { // <<== This line here
    var move = function(e) {
        YAHOO.util.Dom.setXY('foo', YAHOO.util.Event.getXY(e));
    };
    YAHOO.util.Event.on(document, "click", move);
})();  // <<== And this line here
</SCRIPT>
 
instead of simply this...
 
<SCRIPT>
    var move = function(e) {
        YAHOO.util.Dom.setXY('foo', YAHOO.util.Event.getXY(e));
    };
    YAHOO.util.Event.on(document, "click", move);
</SCRIPT>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This technique is frequently used with the topic of closure.
By defining, and calling this function, objects can be instantiated that
can have a lifetime longer than a simple called function.