A common practice in modern times is to compress all the JS files into one and connect them to <head> . Working with such a structure, at what point and how should variables and classes be initialized so that the memory consumption is optimal?
I will explain the issue.
Of course, we can initialize in onload all the variables we work with, but we don’t need all the variables on a specific page of the site / application: why initialize the variables associated with the submission form if we are on the main page? And if these are DOM objects, then we will not be able to initialize them, being on a page where there are none (if we, of course, do not completely generate HTML in JS). It turns out that this option is not suitable.
In javascript question : is it worth avoiding the style of writing code “function in function”? I proposed a code structure that allows you to initialize variables only when they are needed. At the time of writing the current question, the answers like "yes, apply this approach, because <justification>" or "no, such an approach should be avoided, because <justification>" did not exist, but I myself used the data without any particular problems while did not start working with ES2015-classes.
These classes cannot be used until the declaration. So how do you first write classes inside a function (to initialize them only when they are needed), and then write auxiliary ones inside the same function - a very bad tone, the question arose: how should these classes be initialized as necessary?
headernow moveton. 2 - Read about modular js - ThisMan