Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Improved grammar. Improved spelling. Improved punctuation. Improved wording.
Source Link
Pang
  • 10.2k
  • 146
  • 87
  • 126

Normally javascrypt, JavaScript code has global scope in the application. When we declare global variable in it, there is a chance for using the samethe same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

Two wayways we can create IIFE

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

OR

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

In the above code snippet above, "var app" is a local variable now.

Normally javascrypt code has global scope in the application. When we declare global variable in it there is a chance for using the same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

Two way we can create IIFE

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

OR

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

In the above code snippet "var app" is a local variable now.

Normally, JavaScript code has global scope in the application. When we declare global variable in it, there is a chance for using the same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

Two ways we can create IIFE

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

OR

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

In the code snippet above, "var app" is a local variable now.

Source Link
Rinoy Ashokan
  • 1.6k
  • 20
  • 17

Normally javascrypt code has global scope in the application. When we declare global variable in it there is a chance for using the same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

Two way we can create IIFE

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

OR

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

In the above code snippet "var app" is a local variable now.

lang-js

AltStyle によって変換されたページ (->オリジナル) /