0

I have recently started the Angular.js tutorial which shows code written inside of an Immediately Invoked Function Expression (IIFE). I was wondering why should we put our JavaScript inside such a construct? Is it because of security reasons?

Eg: Why this:

(function(){
    var app = angular.module("AppName", [])
})();

And not just this:

var app = angular.module("AppName", [])
JAAulde
  • 19,250
  • 5
  • 52
  • 63
siannone
  • 6,617
  • 15
  • 59
  • 89
  • Sorry, didn't know that IIFE was another way of calling it. I'll vote to close the question. – siannone Sep 19 '14 at 16:50
  • 1
    It's all good, I just wanted to clarify terms, and the other chap noticed that with the clarified terms this had already been asked. A closure is really something different than an IFFE, though IIFE's are a way to create a closure. – JAAulde Sep 19 '14 at 16:51
  • Notice, that the examples are doing totally different things. – Teemu Sep 19 '14 at 17:02

2 Answers2

1

Having app as a global variable could cause conflicts

Mark
  • 3,137
  • 4
  • 39
  • 76
1

It prevents you from creating a global variable.

Brian Lewis
  • 5,739
  • 1
  • 21
  • 28