Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - ( function () {} () )? [duplicate]

Tags:

javascript

Possible Duplicate:
javascript function vs. ( function() { … } ());

I'm seeing this pattern in several of TodoMVC's JS source:

(function() {
    // ...
    // ...
}());

What's the specific meaning of this pattern? Note that it is not the self-invoking function which is (function() {})();

like image 494
gsklee Avatar asked Feb 17 '26 03:02

gsklee


1 Answers

What's the specific meaning of this pattern? Note that it is not the self-invoking function which is (function() {})();

You're incorrect, it is an Immediately Invoked Function Expression (IIFE). The parenthesis are just in a different place, but they bind the exact same way.

People often do it in the way you've described to get it to validate JSLint.

It's used for scoping, as JavaScript only has function and global scope (ignoring let).

like image 62
alex Avatar answered Feb 18 '26 17:02

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!