Some thoughts on javascript module loading technology

  • 2020-03-30 04:24:24
  • OfStack

Not long ago, an online user asked me about using requireJs and seajs in the front end. I asked him if your company had any javascript libraries or javascript frameworks written by itself before. His answer was nothing.

The problem of the Internet has caused me to javascript module loading technology of thinking, the article I gave his writing of the basic structure of a javascript library, actually writing this article is a cause because I want to use requirejs or seajs such technology to redesign I write javascript library of basic model, after when I understand the technology, I found that using module loading system to solve the universal code and javascript library business code decoupling problem, it is not correct, The scope of the module loading system is to solve the dependency problem between different javascript libraries, not to help you develop a javascript library.

So what is a javascript module loading system?

Module system is mainly to solve the naming conflicts of operation objects in different javascript libraries and the dependency between different javascript libraries. The module loading system is for large web front-end applications or giant web front-end applications.

General giant web front-end application page, the page function is rich, the business is very confused, and over time, the function of the page changes will happen often, so often lead to front-end developers to develop for new function of functional modules, but in the actual business between each function module of function and possibly mutual penetration, interdependence, complicated relationship, when the page is complex, the front end of the relationship between library appears difficult to manage and control the problem, this time module loading system will come in handy.

For most programmers, can independently bear so big web front-end application is not too much, and the development opportunity to small and medium-sized web front-end application will be much more, such as the web project enterprise, this project USES the javascript library rarely, the kinds of dependency relationship is very good control of each library, there is no need to introduce what module management system is necessary, even if a lot of small and medium sized Internet company's web page, estimated also won't than enterprise web application front end so complex, so it's a good relationship between modules or javascript library management. Actually like these small and medium-sized application is aimed at a certain or a particular scene, so I feel in the face of such personal web front-end project in the end we could form a separate javascript library, the library should have the characteristics of this type and the jQuery library is similar: a main library with several plug-in library mode, the main purpose is to solve the problem of generality, it should be for reuse and migration, and the purpose of the plug-in library often associated with business code, but in order to distinguish the main library and the plug-in library the scope of the problem, so I'm in the garage with a namespace.

Javascript module loading and hadoop technologies have some similarities, that is they are all in very large system technology, they only under certain conditions can play their role, so these technologies come from big Internet companies to launch, because big Internet companies to change large complicated with application must be to solve the problem, after when your system is still in its infancy, the use of these technologies tend to be cautious, we should find the simplest the most effective way to solve our problems, if you think that after the system will be more and more big, Then you should keep the interfaces for later use of these technologies, if you use them too early, you are more likely to refactor your code as the system scales up.

For module loading system, it is the most appropriate scenario to solve large web front-end application module between the decoupling problem, if we just new immediately write a javascript file using module loading technology, isn't this a little suspected of abuse of technology, we use a technology before should not just consider how to use it, how to use, should consider to use it even have the value of the problem.

Finally, I want to say is, I think the small and medium-sized web front-end application to production deployment, because javascript is not the most complex, so all external javascript files are packaged into an external javascript file is best, the advantage is to reduce the number of HTTP requests, using the module loading technology will allow you to pack file operations very troublesome, can't even do it (like requirejs and seajs module is based on file for the unit, each module is a separate document), and solve this purpose is to reduce HTTP.


Related articles: