Exporting variables and functions in modules via closures in JavaScript

Sometimes you need to make certain variables and functions of a module accessible from the outside. Let's see how it's done. Let's say we have the following module:

;(function() { let str = 'a module variable'; function func() { alert('a module function'); } })();

Let's export our function func. To do this, we write it into the property of the window object embedded in the browser:

;(function() { let str = 'a module variable'; function func() { alert('a module function'); } window.func = func; })();

Now we can call our function from outside the module:

;(function() { let str = 'a module variable'; function func() { alert('a module function'); } window.func = func; })(); window.func(); // shows 'a module function'

In this case, it is not necessary to call the function as a property of the window object:

;(function() { let str = 'a module variable'; function func() { alert('a module function'); } window.func = func; })(); func(); // shows 'a module function'

Given the following module:

;(function() { let str1 = 'a module variable'; let str2 = 'a module variable'; let str3 = 'a module variable'; function func1() { alert('a module function'); } function func2() { alert('a module function'); } function func3() { alert('a module function'); } })();

Export one of the variables and any two functions to the outside.

Object export

Let's say we have the following module:

;(function() { function func1() { alert('a module function'); } function func2() { alert('a module function'); } function func3() { alert('a module function'); } })();

Let's say we want to export all three functions to the outside. In this case, in order not to produce unnecessary function names outside the module, it is better to write all the functions into one object and export this object:

;(function() { function func1() { alert('a module function'); } function func2() { alert('a module function'); } function func3() { alert('a module function'); } window.module = {func1: func1, func2: func2, func3: func3}; })();

Since the names of keys and variables are the same, the object with functions code can be simplified:

;(function() { function func1() { alert('a module function'); } function func2() { alert('a module function'); } function func3() { alert('a module function'); } window.module = {func1, func2, func3}; })();

You can also go the other way. We will write functions to the object immediately when describing the function, like this:

;(function() { let module = {}; module.func1 = function() { alert('a module function'); } module.func2 = function() { alert('a module function'); } module.func3 = function() { alert('a module function'); } window.module = module; })();

Given the following module:

;(function() { let str1 = 'a module variable'; let str2 = 'a module variable'; let str3 = 'a module variable'; function func1() { alert('a module function'); } function func2() { alert('a module function'); } function func3() { alert('a module function'); } function func4() { alert('a module function'); } function func5() { alert('a module function'); } })();

Export the object with the first five functions and the first two variables to the outside.

enru