0

Sorry about this being so vague, but I'm sure how else to ask this, simply because I don't know what it is. I've seen this once before, but I can't remember what it's doing or where to look for it.

What is this code doing with the outer parentheses and "window" thing?

(function(angular){
//some code
})(window.angular);

I really am sorry. After I get reference to some official documentation or something, I'll delete the post.

Skello
  • 143
  • 1
  • 10
  • 2
    There's an ambiguity in the grammar with the `function` keyword. When it's the first token in a statement, the statement is taken to be a function declaration. In the code you posted, the function instantiation needs to be interpreted as a function *expression*, so the parentheses flip the parser into "expression parsing" mode and everything works. – Pointy Aug 11 '17 at 00:59
  • 3
    It looks like an [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) – zer00ne Aug 11 '17 at 01:00
  • 1
    @MrGeek: It appears so, but I didn't even know what this what this was, so there's no way I would have queried "Anonymous function syntax" – Skello Aug 11 '17 at 01:05
  • @zer00ne: Thanks it looks like that's what it is. – Skello Aug 11 '17 at 01:05
  • 1
    "After I get reference to some official documentation or something, I'll delete the post." Please don't do that. Even if this question gets closed as a duplicate, it may still be useful for future users who might also not know to search for "IIFE or "encapsulated anonymous function". – Daniel Beck Aug 11 '17 at 01:08
  • @DanielBeck, it's up to you guys. I just don't want to get threatened with a "Stop-posting-redudant-stuff"-kind of reprimand. – Skello Aug 11 '17 at 01:10
  • Duplicates are fine -- beneficial even -- if they're not obvious "you should have found the original on your own" ones (which this definitely isn't.) That's why they're still included in search: they're signposts pointing to the original. – Daniel Beck Aug 11 '17 at 01:22

1 Answers1

2

This is an example of IIFE (Immediately Invoked Function Expressions). Take a look at here for more explanation.

The first part

(function(angular){
//some code
})

is an anonymous function (it does not have a name). Since you want to execute it immediately (when the page/DOM is loaded), you just call it like any other function

(window.angular);

..with a parenthesis, arguments and a semicolon. Your argument (window.angular) is just a global object (which is why it is defined on window scope).

In short, you are executing that body of function with an argument, which is defined globally.

zer00ne
  • 31,838
  • 5
  • 32
  • 53
James Poulose
  • 2,701
  • 1
  • 26
  • 27
  • Okay, so what's the difference between this and the usual "document.ready(function(){})" way of doing this? – Skello Aug 11 '17 at 01:11
  • Sorry. Just read again, for the fifth time. So the document.ready version doesn't expose an opportunity to place parameters into the function, and it doesn't give you the ability to call this function elsewhere in your hedgemaze of javaScript files...if I'm understanding this correctly? – Skello Aug 11 '17 at 01:13
  • @SageKelly As you correctly said, `document.ready` can also be used to initialize/execute code when DOM is loaded. The difference is that you have to remember to do that. IIFEs are automatically executed by the browser. If you are writing a set of functions accessible only by a scoped object (in your case `window.angular`), this is a better way to do it. Third party libraries usually follow this route, because all you have to do is to just include the JS file reference. – James Poulose Aug 11 '17 at 01:23