How does this javascript function work on its own / Structure

I have inherited the application and I see this JavaScript structure everywhere. What is the reason for its creation. I get that this is an anonymous function and that PaymentOffersOverview gets it.

 var PaymentOffersOverview = PaymentOffersOverview || {}; (function(PaymentOffersOverview) { var App = {}; App.Settings = { Id: some Id value // Some Code }; App.ShowLoader = function (message) { // Some Code }; PaymentOffersOverview.App = App; })(PaymentOffersOverview); 

But I do not get the last line. })(PaymentOffersOverview); Why is he being transmitted again?

 // Then the code can be used as follow alert(PaymentOffersOverview.App.Settings.Id); 

Fiddle

+5
source share
1 answer

He does not pass again. On the last line, the closure gets PaymentOffersOverview , because it self- PaymentOffersOverview . If it was not accepted, PaymentOffersOverview within the closure will be undefined .

 // definition of local variable var PaymentOffersOverview = PaymentOffersOverview || {}; (function(param) { // now we are in the context of the closure where param refers to // PaymentOffersOverview, yet PaymentOffersOverview is not defined // within this scope. It is param. // You can use any name here, using the same is just for convinience. var App = {}; App.Settings = { Id: some Id value // Some Code }; App.ShowLoader = function (message) { // Some Code }; param.App = App; })(PaymentOffersOverview); // passing of the variable 
0
source

All Articles