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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By defining, and calling this function, objects can be instantiated that
can have a lifetime longer than a simple called function.