3 ms·
I notice something like this in the snippet: (function() { ... })(); What's that all about?
by scotth 17y ago
I notice something like this in the snippet:
(function() { ... })();
What's that all about?
- mcav 17y agoThat executes the code in a local scope, so that any variables defined within that block don't leak outside of its namespace.
- scotth 17y agoThanks!
- rykov 17y agoThis works even cooler when you want to minify your code: MyLongClassName = function() {}; function(_c) { _c.prototype.method1 = ...; _c.prototype.method2 = ...; }(MyLongClassName); This allows something like YUI compressor to shorten _c local variable for every method definition, rather than store "MyLongClassName" for each one.
- tannerburson 17y agoDefine an anonymous function that takes no arguments, and execute it without assigning it to a variable. It's often used in javascript to keep things out of the global scope.
- scotth 17y agoThe first response was perfectly adequate. Let's keep threads clean. Read other responses before responding.