Why do we wrap Angular app inside a function?

U r s u s

I'm learning Angular and in all the resources I've used so far I saw this in the app.js file:

(function () {

       \\\myAngularModules      

})();

The most commonl explainantion is an unhelpful "it's just good practice".

Questions:

  1. Is wrapping our Angular JS code in a function really good practice? Why?
  2. What sort of function is it and what does it do?

Please give examples where possible.

RyanHirsch

This is what is known as an immediately invoked anonymous function (IIFE). It allows us to create a new function scope and immediately run the code so that no variable or other items we create "leak" out and manipulate the global scope.

Leaking of your code into the global can impact other modules or 3rd party code. This also helps protect your code by making you think about what objects you are using that are not declared in your local scope.

http://gregfranko.com/blog/i-love-my-iife/ as a more detailed explanation that covers the general idea other esoteric things like minification benefits.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap Angular App in a jQuery function

From Dev

Why can't we add properties to a function object inside function body like we do for object literals in javascript?

From Dev

Why can't we define function inside the main function?

From Dev

Why do we use the end Function?

From Dev

Why do we need visibleCells function (UICollectionView)

From Dev

Why do we create a private variable inside a class and then set property?

From Dev

why do we add bin directory to the path inside Environment Variables?

From Dev

Why wrap your angular controller code with anonymous function?

From Dev

Why do you have to wrap a callback with an anonymous function?

From Dev

Why do we need to store Goefence data in android app?

From Dev

Why do we need pull to refresh for mobile app?

From Dev

Why do we need to deploy a meteor app instead of just starting it?

From Dev

Why do we need to store Goefence data in android app?

From Dev

Why do we need to deploy a meteor app instead of just starting it?

From Dev

Why do we must use SQLAlchemy with PostgreSQL in a Flask app development?

From Dev

ReactJS : Why do we use nodeJs in creating react app?

From Dev

In angularjs why we use [] in angular.module() function?

From Dev

Why do we prefer using $q in angular instead of $http

From Dev

Why do we have install Node.js for Angular 2.0?

From Dev

Angular Router: Why do we need both: guards and events?

From Dev

why do we use setTimout function if AJAX is asynchronous?

From Dev

Why do we need to use a sigmoid function when using backpropagation?

From Dev

Why do we need a constant time *single byte* comparison function?

From Dev

Why do we add "javascript:" before calling a function in HTML?

From Dev

Why do we need to specify parenthesis while passing the function to a directive?

From Dev

Why do we pass function arguments as void* in pthread_create?

From Dev

Why do we call completion on main thread in asynchronous function?

From Dev

Why do we need to use a sigmoid function when using backpropagation?

From Dev

Why do we need to specify parenthesis while passing the function to a directive?

Related Related

  1. 1

    Wrap Angular App in a jQuery function

  2. 2

    Why can't we add properties to a function object inside function body like we do for object literals in javascript?

  3. 3

    Why can't we define function inside the main function?

  4. 4

    Why do we use the end Function?

  5. 5

    Why do we need visibleCells function (UICollectionView)

  6. 6

    Why do we create a private variable inside a class and then set property?

  7. 7

    why do we add bin directory to the path inside Environment Variables?

  8. 8

    Why wrap your angular controller code with anonymous function?

  9. 9

    Why do you have to wrap a callback with an anonymous function?

  10. 10

    Why do we need to store Goefence data in android app?

  11. 11

    Why do we need pull to refresh for mobile app?

  12. 12

    Why do we need to deploy a meteor app instead of just starting it?

  13. 13

    Why do we need to store Goefence data in android app?

  14. 14

    Why do we need to deploy a meteor app instead of just starting it?

  15. 15

    Why do we must use SQLAlchemy with PostgreSQL in a Flask app development?

  16. 16

    ReactJS : Why do we use nodeJs in creating react app?

  17. 17

    In angularjs why we use [] in angular.module() function?

  18. 18

    Why do we prefer using $q in angular instead of $http

  19. 19

    Why do we have install Node.js for Angular 2.0?

  20. 20

    Angular Router: Why do we need both: guards and events?

  21. 21

    why do we use setTimout function if AJAX is asynchronous?

  22. 22

    Why do we need to use a sigmoid function when using backpropagation?

  23. 23

    Why do we need a constant time *single byte* comparison function?

  24. 24

    Why do we add "javascript:" before calling a function in HTML?

  25. 25

    Why do we need to specify parenthesis while passing the function to a directive?

  26. 26

    Why do we pass function arguments as void* in pthread_create?

  27. 27

    Why do we call completion on main thread in asynchronous function?

  28. 28

    Why do we need to use a sigmoid function when using backpropagation?

  29. 29

    Why do we need to specify parenthesis while passing the function to a directive?

HotTag

Archive