3

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() {})();

0

1 Answer 1

8

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any advantage wrapping the parenthesis this way over the other way around?
@Kay One passes JSLint, one doesn't. If passing JSLint is important to you, then use the appropriate form.
You ain't gonna get a more correct answer than this. The mention of JSLint makes it bulletproof. Damn that Crockford... always rearing his head in my text editor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.