Several global variables in javascript

This is not a specific problem, but a more theoretical question. Is there a good reason to expand multiple global variables for a single Javascript application ?

I can use and often use for myself one global variable to indicate an object or class that contains the application so that it can be called several times (example below), but I can not think of a single case when additional global variables cannot be replaced by properties object.

An example in which an open variable makes life easier (using closure, it could not be called):

var myGlobalApp = { init: function (args) {...}, methodOne: function () {...}, methodTwo: function () {...}, propertyOne: 'string for example' }; myGlobalApp.init(arg1); myGlobalApp.init(arg2); 

Does anyone know of an instance case where several global variables are needed?

+6
source share
5 answers

I don’t think that it’s ever strictly necessary to have several globals - JavaScript objects can be arbitrarily nested and can often be used as namespaces.

 window.AwesomeModule = { app: { ... }, util: { ... } }; 

In fact, if your application cannot be reused (that is, it is simply accessed by the user), you may not be able to skip any global variables.

 (function() { var AwesomeModule = { ... }; // Do whatever you want - create DOM nodes, bind to events, etc // Just don't bind anything to window })(); 

A more interesting question is whether multiple global values ​​will really be useful, and I would say that it depends on your development style. For example, if we look at C # and .NET in general, we will see that the entire infrastructure (more or less), namespaces and everything, is located below the top System namespace.

Of course, if you are going to create a huge JavaScript application with several components, I would definitely not recommend such a nested structure (in addition to being cumbersome, JavaScript JavaScript attributes have a certain cost of execution, which can add up).

... Despite this, the JavaScript landscape is not trimmed so well. A simple check of global attributes gives about 56 elements on my machine on a blank page (Chrome works).

 var i = 0; for (var prop in window) { if (window.hasOwnProperty(prop)) { i++; } } 

So, although we can and should minimize our own global use, the JS environment that exists today (especially when using external libraries) is often associated with the distribution of global variables. Example: StackOverflow has about 144 common global bindings.

+4
source

Typically, client-side libraries for Javascript accept at least one global because it would be awkward to require the user to use a module library.

0 global variables - this is an opportunity in ES6 with first-class modules.

0
source

I would say that the general rule applicable to Javascript, and, in this regard, any other language should minimize the case for global variables. I am sure that there are times when more than one global variable is required, but this should be avoided as much as possible. When creating a library such as jQuery, you usually only need to open one entry point to that library.

In addition, global variables pollute the Javascript namespace, and this can lead to conflicts with other people's code.

0
source

The only circumstance in which I set up several globals was for an application that would run several instances, each of which needed a link to one unified resource for use onclick callbacks. Even in this case, with a little extra effort, I could save the resource contained in the application, and did not need an additional global one.

In short, in rare cases it is useful / quick and dirty, but never needed.

0
source

Does anyone know of an instance case where several global variables are needed?

Global variables are never needed, any JavaScript code that uses global variables can be rewritten so that it does not.

Even in your example, global variables are not needed; instead, you can use the self-executing function, as shown below:

 (function(arg1, arg2) { var init = function (args) {...}; var methodOne = function () {...}; var methodTwo = function () {...}; var propertyOne = 'string for example'; init(arg1); init(arg2); })(arg1, arg2); 

Obviously, libraries often expose a global variable to use this library, for example jQuery creates jQuery and $ global variables. But for a single JavaScript application, global variables are never needed.

0
source

Source: https://habr.com/ru/post/951233/


All Articles